10

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.

Jackson Dean Goodwin
  • 617
  • 3
  • 11
  • 20
  • Did you set the DataContext? – Bob. Dec 11 '12 at 18:53
  • 1
    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}"` – Jackson Dean Goodwin Dec 21 '12 at 17:54
  • I have found a solution to your question [http://stackoverflow.com/questions/898852/specify-command-for-menuitem-in-a-datatemplate/18362041#18362041][1] [1]: http://stackoverflow.com/questions/898852/specify-command-for-menuitem-in-a-datatemplate/18362041#18362041 – Jacksquad Aug 21 '13 at 15:50

3 Answers3

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}}" />
LPL
  • 16,827
  • 6
  • 51
  • 95
  • 1
    Are you sure it doesn't get the `DataContext`? In my test it seems to be inheriting the DataContext as you would expect ... – McGarnagle Dec 11 '12 at 19:52
  • I tried your code for the Command="..." but it did not work - just as before - nothing happed. – Jackson Dean Goodwin Dec 11 '12 at 19:58
  • @dbaseman According to [this](http://msdn.microsoft.com/en-us/library/ms749018.aspx#PopupandtheVisualTree) Popup creates its own visualtree. This means no DataContext inheritance for ContextMenu which is placed in a Popup. – LPL Dec 11 '12 at 20:11
  • @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. – LPL Dec 11 '12 at 20:13
  • 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. – Agent007 Nov 09 '13 at 06:03
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.

SnakE
  • 2,355
  • 23
  • 31
-1

for binding cross visual tree, refer to

Binding Visibility for DataGridColumn in WPF

or jsut try search BindingProxy

Community
  • 1
  • 1