How do you bind a command to a MenuItem (WPF)?
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}}" />
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.
for binding cross visual tree, refer to
Binding Visibility for DataGridColumn in WPF
or jsut try search BindingProxy