Bort
Bort

Reputation: 835

Command on 'Parent' ContextMenu

I'm creating a context menu in code behind with a submenu. I want the top level menu item to act as a command when pressed, but when I add child menu items, the command behavior seems to be taken over by the expanding behavior. How can I remedy this?

MenuItem item = new MenuItem();
item.Command = DoSomething;
item.Header = "Parent";

MenuItem subItem = new MenuItem();
subItem.Command = DoSomethingElse;
subItem.Header = "Child";

item.Items.Add(subItem);
ContextMenuItems.Add(item);
<Button>
    <Button.ContextMenu>
        <ContextMenu ItemsSource="{Binding ContextMenuItems}" />
    </Button.ContextMenu>
</Button>

<CommandBinding Command="{x:Static DoSomething}"
                Executed="DoSomethingExecuted"/>

<CommandBinding Command="{x:Static DoSomethingElse}"
                Executed="DoSomethingElseExecuted"/>

Upvotes: 1

Views: 203

Answers (1)

brunnerh
brunnerh

Reputation: 185489

I want the top level menu item to act as a command when pressed

You should not be doing this, ever.

(It's a matter of guidelines, consistency and user-expectancies)

Upvotes: 4

Related Questions