69

Has anybody else noticed that Bindings with ElementName do not resolve correctly for MenuItem objects that are contained within ContextMenu objects? Check out this sample:

<Window x:Class="EmptyWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    x:Name="window">
    <Grid x:Name="grid" Background="Wheat">
        <Grid.ContextMenu>
            <ContextMenu x:Name="menu">
                <MenuItem x:Name="menuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
                <MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
                <MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
                <MenuItem Header="Menu Item" Tag="{Binding ElementName=menuItem}" Click="MenuItem_Click"/>
            </ContextMenu>
        </Grid.ContextMenu>
        <Button Content="Menu" 
                HorizontalAlignment="Center" VerticalAlignment="Center" 
                Click="MenuItem_Click" Tag="{Binding ElementName=menu}"/>
        <Menu HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <MenuItem x:Name="anotherMenuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
            <MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
            <MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
            <MenuItem Header="Menu Item" Tag="{Binding ElementName=anotherMenuItem}" Click="MenuItem_Click"/>
        </Menu>
    </Grid>
</Window>

All of the bindings work great except for the bindings contained within the ContextMenu. They print an error to the Output window during runtime.

Any one know of any work arounds? What's going on here?

Josh G
  • 14,068
  • 7
  • 62
  • 74

6 Answers6

59

I found a much simpler solution.

In the code behind for the UserControl:

NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this));
Josh G
  • 14,068
  • 7
  • 62
  • 74
  • 1
    It works in 4.5 too. Amazing solution, works very well. Thanks. – Andre Soares Dec 17 '15 at 18:38
  • 4
    Works for 4.5.2 too. Take care... It fix the binding using "ElementName" but it does not fix the binding using "RelativeSource FindAncestor". – Eric Ouellet Aug 17 '16 at 19:21
  • 1
    Source={x:Reference Name=Root} from Marc answer is much better BTW because its don't need code behind and may be used in resource dictionaries – Anton Shakalo Sep 13 '16 at 15:50
33

As said by others, the 'ContextMenu' is not contained in the visual tree and an 'ElementName' binding won't work. Setting the context menu's 'NameScope' as suggested by the accepted answer only works if the context menu is not defined in a 'DataTemplate'. I have solved this by using the {x:Reference} Markup-Extension which is similar to the 'ElementName' binding but resolves the binding differently, bypassing the visual tree. I consider this to be far more readable than using 'PlacementTarget'. Here is an example:

<Image Source="{Binding Image}">       
    <Image.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Delete" 
                      Command="{Binding Source={x:Reference Name=Root}, Path=DataContext.RemoveImage}"
                      CommandParameter="{Binding}" />
        </ContextMenu>
    </Image.ContextMenu>
</Image>

According to the MSDN-documentation

x:Reference is a construct defined in XAML 2009. In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features.

whatever that means... Works for me, though.

Marc
  • 12,706
  • 7
  • 61
  • 97
  • Simple, elegant and works at first try : this should marked as the solution (really better than PlacementTarget) – nmariot Mar 25 '19 at 11:27
  • That quote from the MSDN documentation seems to indicate to me that using `x:Reference` does not allow Visual Studio (or whatever compiles XAML) to compile the XAML into a binary form. I wonder what the performance hit is. – kfreezen Apr 02 '19 at 15:19
  • Impressive, it worked for me ;). Probably the easiest and elegant solution ! – Kino101 Jan 25 '21 at 14:35
  • 1
    If you have your ContextMenu inside a resources-block, the designer throws with an "XamlParseException: Unresolved reference 'Root'". Running the program works, tho. – MHolzmayr Nov 10 '21 at 13:51
  • 3
    If the `Root` object is a parent of the `MenuItem` this fails at runtime with an error "Cannot call MarkupExtension.ProvideValue because of a cyclical dependency." – StayOnTarget Apr 20 '22 at 14:53
22

Here's another xaml-only workaround. (This also assumes you want what's inside the DataContext, e.g., you're MVVMing it)

Option one, where the parent element of the ContextMenu is not in a DataTemplate:

Command="{Binding PlacementTarget.DataContext.MyCommand, 
         RelativeSource={RelativeSource AncestorType=ContextMenu}}"

This would work for OP's question. This won't work if you are inside of a DataTemplate. In these cases, the DataContext is often one of many in a collection, and the ICommand you wish to bind to is a sibling property of the collection within the same ViewModel (the DataContext of the Window, say).

In these cases, you can take advantage of the Tag to temporarily hold the parent DataContext which contains both the collection AND your ICommand:

class ViewModel
{
    public ObservableCollection<Derp> Derps { get;set;}
    public ICommand DeleteDerp {get; set;}
} 

and in the xaml

<!-- ItemsSource binds to Derps in the DataContext -->
<StackPanel
    Tag="{Binding DataContext, ElementName=root}">
    <StackPanel.ContextMenu>
        <ContextMenu>
            <MenuItem
                Header="Derp"                       
                Command="{Binding PlacementTarget.Tag.DeleteDerp, 
                RelativeSource={RelativeSource 
                                    AncestorType=ContextMenu}}"
                CommandParameter="{Binding PlacementTarget.DataContext, 
                RelativeSource={RelativeSource AncestorType=ContextMenu}}">
            </MenuItem>
  • I think the relevant point that you are making here is that you can use tags and relative source bindings to get at data in another place in the visual tree. – Josh G Mar 19 '11 at 20:12
  • This isn't really related to MVVM. I only use ElementName bindings when I'm trying to tie two view related controls together outside of the VM. This is a good solution for binding context menu items to commands on a VM. A good alternative is to use a routed command that ties into the VM. A good example of this is Josh Smith's CommandSink class. – Josh G Mar 19 '11 at 20:15
  • the trick with `Tag` is good! It works, and doesn't drive VS2022 XAML designer crazy (as the approach with `Binding Source={x:Reference Name=Root}`) – AntonK Mar 27 '23 at 16:05
6

Context menus are tricky to bind against. They exist outside the visual tree of your control, hence they can't find your element name.

Try setting the datacontext of your context menu to its placement target. You have to use RelativeSource.

<ContextMenu 
   DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"> ...
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Josh
  • 221
  • 1
  • 3
  • Setting the DataContext to the PlacementTarget would effect ElementName bindings? I think the DataContext is only used for Bindings that have no Source, RelativeSource, or ElementName property set. – Josh G Jun 18 '09 at 18:52
  • Setting an ElementName property will only work if the layout manager can find the associated element by navigating up the visual tree. Context menus do not exist inside the visual tree of the control to which they are added. You must set the datacontext of the context menu so the layout manager can navigate up the visual tree of its placement target to find the associated element. – Josh Jun 18 '09 at 20:00
  • Adding the DataContext to the above example didn't fix the problem. I still got the following error in the Output window: "System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=window'. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name='menuItem'); target property is 'Tag' (type 'Object')" – Josh G Jun 18 '09 at 20:53
  • hmmm...looking back over my code I have only done this by setting the relative source directly in the binding, I thought setting the DataContext would be simpler. Here is what worked for me: CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" The syntax will obviously be different, but perhaps setting the source attribute of your binding to the placement target will work? – Josh Jun 18 '09 at 21:03
  • OK, that makes sense now. You are changing the ElementName reference to a RelativeSource reference through the ContextMenu. Thanks for the thoughts. – Josh G Jun 19 '09 at 12:09
4

After experimenting a bit, I discovered one work around:

Make top level Window/UserControl implement INameScope and set NameScope of ContextMenu to the top level control.

public class Window1 : Window, INameScope
{
    public Window1()
    {
        InitializeComponent();
        NameScope.SetNameScope(contextMenu, this);
    }

    // Event handlers and etc...

    // Implement INameScope similar to this:
    #region INameScope Members

    Dictionary<string, object> items = new Dictionary<string, object>();

    object INameScope.FindName(string name)
    {
        return items[name];
    }

    void INameScope.RegisterName(string name, object scopedElement)
    {
        items.Add(name, scopedElement);
    }

    void INameScope.UnregisterName(string name)
    {
        items.Remove(name);
    }

    #endregion
}

This allows the context menu to find named items inside of the Window. Any other options?

Josh G
  • 14,068
  • 7
  • 62
  • 74
1

I'm not sure why resort to magic tricks just to avoid a one line of code inside the eventhandler for the mouse click you already handle:

    private void MenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // this would be your tag - whatever control can be put as string intot he tag
        UIElement elm = Window.GetWindow(sender as MenuItem).FindName("whatever control") as UIElement;
    }
Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
  • 1
    Doing it this way doesn't allow the menu item to become disabled automatically according to the bound command though. So while it works for execution, you would have to add more code to also disable/enable the menu item accordingly when it's loaded. Not that this is bad, it just brings back bad memories of WinFoms UI code spaghetti for lots of people. – jpierson Dec 05 '12 at 07:45