MenuItem Commands in Custom Controls RRS feed

  • General discussion

  • Hey guys;

    Is there anything special I need to do when setting up commands on a menu I have inside of a custom control?  The reason I ask is because I created this custom control that has a menu inside of it with commands attached to each menu item.  Everything works fine until I put it inside of a page that already has a menu in it.  The page's menu works fine, but the ones on my custom control has all its menu items disabled.  I threw a breakpoint in my control's menu's CanExecute function and it never gets trigger when its inside of that page.  I'm guessing its a scoping issue but I don't know what I'm doing wrong.  I'll post some code below, hopefully you can help!  Thanks!!!!!


    <MenuItem Header="group1">
    <MenuItem Header="item1" Command="{x:Static local:CSControl.MenuItemToggleCommand}"/>

    // command execute/canexecute methods
    public static RoutedCommand MenuItemToggleCommand = new RoutedCommand();
    public static void ExecutedMenuItemToggleCommand(object sender, ExecutedRoutedEventArgs e) {
                // do stuff

    public static void CanExecuteMenuItemToggleCommand(object sender, CanExecuteRoutedEventArgs e) {
        Control target = e.Source as Control;
        if(target != null)
            e.CanExecute = true;
            e.CanExecute = false;

    // init
    CommandBindings.Add(new CommandBinding(MenuItemToggleCommand,

    • Changed type Marco Zhou Friday, August 15, 2008 9:33 AM OP doesn't revert back
    Friday, August 8, 2008 5:38 PM

All replies

  • Could you please send your test project to me at v-mazho at microsoft dot com for repro?

    Tuesday, August 12, 2008 9:00 AM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by clicking the "Options" link at the top of your post, and selecting "Change Type" menu item from the pop menu. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


    Thank you!

    Friday, August 15, 2008 9:33 AM