Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here is the general pattern that I use....</p> <p>first, keep your commands in thier own static class, this promotes reuse,etc....</p> <pre><code>public static class MyCommands { public static RoutedUICommand CmdFoo = new RoutedUICommand("CmdFoo", "CmdFoo", typeof(MyCommands)); } </code></pre> <p>second, register the command in the control/window/etc. you want to use it in, normally in the constructor</p> <pre><code>public MyControl { public MyControl() { CommandBindings.Add( new CommandBinding( MyCommands.CmdFoo, // this is the command object XCutFooCommand, // execute CanXCuteFooCommand));// can execute? } </code></pre> <p>third, create your handlers in the control/window/etc.....</p> <pre><code> public void CanExecuteRerollCommand(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; // can this command be executed? e.Handled = true; // has this event been handled? } public void ExecuteRerollCommand(object sender, ExecutedRoutedEventArgs e) { // do stuff } } </code></pre> <p>lastly, your xaml ought to look like this:</p> <pre><code> &lt;ContextMenu&gt; &lt;ContextMenu.CommandBindings&gt; &lt;CommandBinding Command="foo:MyCommands.CmdFoo" CanExecute="CanExecuteRerollCommand" Executed="ExecuteRerollCommand" /&gt; &lt;/ContextMenu.CommandBindings&gt; &lt;MenuItem Header="Reroll" Command="foo:MyCommands.CmdFoo"/&gt; &lt;/ContextMenu&gt; </code></pre> <p>notice that there is no binding. Also, notice the <code>&lt;CommandBinding&gt;</code> in the <code>&lt;ContextMenu&gt;</code>. here is a reference.... <a href="http://www.wiredprairie.us/journal/2007/04/commandtarget_menuitem_context.html" rel="noreferrer">http://www.wiredprairie.us/journal/2007/04/commandtarget_menuitem_context.html</a></p> <p>the command being disabled is addressed at <a href="http://www.wpftutorial.net/RoutedCommandsInContextMenu.html" rel="noreferrer">this site</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload