I have a project which is throwing some data binding errors. One example is:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
My question is whether there is a way to determine where this binding is actually declared (either it is declared in XAML or in code).
What I've tried so far:
- added a debug trace for the System.Windows.Data namespace with level set o All; this did not produce any more useful information
- tried doing a text-search in the project for the word Binding in hopes of locating all binding expressions that have the Path set to
HorizontalContentAlignment
; I found only one and removed it but I'm still getting the message which seems to indicate that that was not the faulty one..
Do you know of any other tricks to make WPF spit out some more useful information about where exactly is this binding declared?
UPDATE
After a little bit more searching I'm pretty sure this is somehow caused by a style being applied to a MenuItem
. However, I'm still not able to pin-point the location where the faulty binding is being declared..
UPDATE 2
I found the problem. However the question still remains since finding the issue was mostly a matter of searching in the dark based on the limited info in the error message.
As it turns out, the binding is declared in a style. And the style is not in my application. It's probably the default style for MenuItem
. So to fix the issue for now I've just manually set the HorizontalContentAlignment
on all MenuItems
. The reason for the error is somehow related to order of operations as this MenuItem
is generated in code. I'll post a new question on that separately.
So, for now, the moral of the story is that I feel that there needs to be a better mechanism to determine where the faulty binding is declared. I'd like to see something like a stack trace for bindings..
I'm keeping the question open for a little while longer in case somebody knows of any other tools or methods of determining the place where a binding is declared in code or markup.
I've posted another question regarding the style/binding being applied to the MenuItems
here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…