WPF MenuItem.Command binding to ElementName results to System.Windows.Data Error: 4 : Cannot find source for binding with reference

You can’t bind using element name from a context menu. The link is broken between the context menu and its placement target. You can get around it using a couple of tricks though…

  1. Use RoutedUICommands with a command binding on the UserControl, then no binding is needed.
  2. Use the placement target binding on the context menu’s DataContext. This allows you to at least get the data context of the element the context menu appears on to the context menu.

    DataContext=”{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.DataContext}”

  3. (and i think this is what you want) You can access a static resource, ElementSpy lets you link to the window using a static resource so you can then use a defacto ElementName bindings.

Leave a Comment