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

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.

Leave a Comment