Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Is there a way to determine where a WPF Binding is declared/created?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When debugging WPF binding errors, I find it easiest to break up the error by the semicolons, and start from the end

  1. System.Windows.Data Error: 4 :
  2. Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment;
  3. DataItem=null;
  4. target element is 'MenuItem' (Name='');
  5. target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

So starting from the end:

  • #5 tells you what property contains the binding that is failing. In your case, it's HorizontalContentAlignment

  • #4 is the element containing the failing property, which is a MenuItem without a Name property to identify it by

    So somewhere you have a <MenuItem HorizontalContentAlignment="{Binding ...}" /> that is causing the binding error.

  • #3 is the DataItem, or DataContext, that is behind the target element. It appears to be null for you, but that's not a problem since it looks like your binding isn't referencing the DataContext.

    But this does suggest that the MenuItem is not part of your regular VisualTree, since typically the DataContext is inherited from the parent object.

  • #2 contains the actual binding error and information about the binding. It can actually be further broken up into multiple parts.

    • Cannot find source for binding

    • with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''.

    • BindingExpression:Path=HorizontalContentAlignment;

    "Cannot find source" means the binding can't find the source object to bind to, and in your case, that source object should be {RelativeSource AncestorType={x:Type ItemsControl} (FindAncestor and AncestorLevel=1 are defaults for a RelativeSource, so I'm ignoring those)

    And the last part of #2 shows the Path you are trying to bind to: HorizontalContentAlignment

So to put it all together, somewhere in your code there is a <MenuItem> which is trying to bind its HorizontalContentAlignment to an ItemsControl.HorizontalContentAlignment, but the binding can't find the ItemsControl.

You're using a RelativeSource FindAncestor binding to find the ItemsControl, which searches up the visual tree to find the closest ItemsControl, and it's not finding one so there must be no ItemsControl higher up in the VisualTree hierarchy from the MenuItem.

I often see this problem with ContextMenus because they are not part of the same VisualTree as the rest of your XAML code. (To reference the object in the main VisualTree that a ContextMenu is attached to, you can use the PlacementTarget property, like this example.

Once you understand the binding error, its often easy to find the source of it in your XAML.

Depending on my application size, I usually do one of the following:

  • Search the application for the "target element" from the binding error (in your case, MenuItem), and see if any of them are setting the "target property" (HorizontalContentAlignment) with a binding

  • Search the application for the "target property" from the binding error (HorizontalContentAlignment) to find the binding causing this problem

  • Search the application for something fairly unique from the binding text shown in the binding error. In your case, you could try searching on {x:Type ItemsControl} which would be part of your RelativeSource binding, and there shouldn't be too many search results for such a phrase.

  • Use a 3rd party tool like Snoop or WPF Inspector to track down the binding error at run time.

    I've only used Snoop before, but to use it you need to startup your application and run Snoop against it to inspect your application's VisualTree while it's running. You can then search the VisualTree by typing something like "MenuItem" in the search bar to filter the Visual Tree for all MenuItems, then look through their properties to find out which one has a binding error (the HorizontalContentAlignment property will be highlighted in red because of the binding error).

    It should be noted that if if your MenuItem is inside a ContextMenu, then you need to open that ContextMenu for the MenuItems to be drawn and show up in Snoop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...