How do you bind a command to a MenuItem (WPF)?
Asked Answered
P

3

11

Here is my code from the View.xaml.cs:

private RelayCommand _closeCommand;
public ICommand CloseCommand
{
    get
    {
        if (_closeCommand == null)
        {
            _closeCommand = new RelayCommand(param => this.OnClose());
        }
        return _closeCommand;
    }
}

public void OnClose()
{
    Close();
}

And here is some code from my View.xaml:

<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Name="menuItem_Close" Header="Close" Command="{Binding CloseCommand}" />
    </ContextMenu> 
</Window.ContextMenu>

When I run the program and select the close menu item, nothing happens. The CloseCommand code doesn't even get executed.

Persnickety answered 11/12, 2012 at 18:47 Comment(3)
Did you set the DataContext?Wrennie
I found the solution to my problem. I was using a ViewModel that had a property in it that was another ViewModel type and I needed to scope down to that propery by doing this: Command="{Binding ActiveVM.CloseCommand}"Persnickety
I have found a solution to your question [#899352 [1]: #899352Splenectomy
H
11

ContextMenu is not part of the VisualTree, that's why the DataContext will not be inherited. Here ContextMenu.PlacementTarget is some kind of relay to get the Window:

<MenuItem Name="menuItem_Close" Header="Close"
          Command="{Binding Path=PlacementTarget.DataContext.CloseCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
Hubie answered 11/12, 2012 at 19:38 Comment(5)
Are you sure it doesn't get the DataContext? In my test it seems to be inheriting the DataContext as you would expect ...Gudgeon
I tried your code for the Command="..." but it did not work - just as before - nothing happed.Persnickety
@dbaseman According to this Popup creates its own visualtree. This means no DataContext inheritance for ContextMenu which is placed in a Popup.Hubie
@JacksonDeanGoodwin Any binding errors? Are you sure that DataContext is set for Window? Maybe try a Button with your Command binding in Window directly for that.Hubie
Thanks for this. How should I bind a static ICommand to a MenuItem not using a ViewModel binding? I am currently using (which is not working) Command="{x:Static ...}" but that doesn't seem to work with the solution you have provided here. Please help.Whitsuntide
P
0

Old question, new answer. For me the problem was that GalaSoft.MvvmLight.Command.RelayCommand didn't support closures for the action. RelayCommand stores a weak reference to the action so a closure gets deallocated almost immediately. The action must be a model method or be retained in some other way.

Petrarch answered 20/9, 2016 at 19:23 Comment(0)
V
-1

for binding cross visual tree, refer to

Binding Visibility for DataGridColumn in WPF

or jsut try search BindingProxy

Valli answered 8/4, 2016 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.