here is the generic template i use ....
first save your commands in your static class, this encourages reuse, etc.
public static class MyCommands { public static RoutedUICommand CmdFoo = new RoutedUICommand("CmdFoo", "CmdFoo", typeof(MyCommands)); }
second, register the command in the control / window / etc. you want to use it, as a rule, in the constructor
public MyControl { public MyControl() { CommandBindings.Add( new CommandBinding( MyCommands.CmdFoo,
third, create handlers in the control / window / etc .....
public void CanExecuteRerollCommand(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true;
Finally, your xaml should look like this:
<ContextMenu> <ContextMenu.CommandBindings> <CommandBinding Command="foo:MyCommands.CmdFoo" CanExecute="CanExecuteRerollCommand" Executed="ExecuteRerollCommand" /> </ContextMenu.CommandBindings> <MenuItem Header="Reroll" Command="foo:MyCommands.CmdFoo"/> </ContextMenu>
note the lack of binding. Also note the <CommandBinding> in <ContextMenu> . here is the link .... http://www.wiredprairie.us/journal/2007/04/commandtarget_menuitem_context.html
disabled command is addressed to this site
Muad'Dib
source share