1
votes

I have a button that when clicked, displays a ContextMenu. In this ContextMenu, I have MenuItems. If the MenuItem is left clicked, it should execute a command. All this behavior works properly as of now with the following code:

<Button.ContextMenu>
    <ContextMenu>
        <ContextMenu.ItemContainerStyle>
            <Style TargetType="{x:Type MenuItem}">
                 <Setter Property="Command" Value="{Binding StartContextMenuCommand}" />
                 <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}" />
            </Style>
        </ContextMenu.ItemContainerStyle>
        <MenuItem Header="{x:Static Name:ContextMenuStartNames.1}"/>
        <MenuItem Header="{x:Static Name:ContextMenuStartNames.2}"/>
        <MenuItem Header="{x:Static Name:ContextMenuStartNames.3}"/>
        <MenuItem Header="{x:Static Name:ContextMenuStartNames.4}"/>
    </ContextMenu>
</Button.ContextMenu>

Now I want each menu item to have a context menu with one MenuItem. Since left clicking invokes a Command on a MenuItem, I want the right click behavior to display the following ContextMenu:

<ContextMenu>
      <MenuItem Header="Set Default"></MenuItem>
</ContextMenu>

I tried putting this as a setter in the ItemContainerStyle. I also tried putting it as a ContextMenu of a MenuItem, but neither of my attempts, among others have worked. I'm sure I could come up with a hacky way to do this, but I want to keep it clean and simple.

2
I don't believe that will have the behavior I want. I want a menu of menu items where I can right click on a menu item and have a context menu show up.jsirr13
You can get a Menu of Menu items , TreeView is a nested Menu and each TreeViewItem has it's own ContextMenu which you can show on whichever event you would like.Rameez Ahmed Sayad
That could work, but TreeViewItem does not contain an IsChecked. So that's behavior I'd have to add. Perhaps it could work, but it doesn't seem to be quite the control I'm looking for.jsirr13

2 Answers

0
votes

Maybe i do not understand your question (my english is not... anything special):D But this should work if you just want multilevel contextmenu:

<ContextMenu>
    <MenuItem Header="Top Level 1">
        <MenuItem Header="Sub Level" />
        <MenuItem Header="Sub Level" />
    </MenuItem>
    <MenuItem Header="Top Level 2">
        <MenuItem Header="Sub Level" />
        <MenuItem Header="Sub Level" />
    </MenuItem>
</ContextMenu>
0
votes

Here's my current fix to this issue, but I don't like it at all. I would like to avoid code behind as much as possible.

<ContextMenu.ItemContainerStyle>
      <Style TargetType="{x:Type MenuItem}">    
          <Setter Property="Command" Value="{Binding StartCommand}" />
          <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}" />
          <Setter Property="ContextMenu"> 
             <Setter.Value>
                <ContextMenu StaysOpen="True">
                    <MenuItem Header="Set As Default"/>
                </ContextMenu>
             </Setter.Value>
          </Setter>
          <EventSetter Event="PreviewMouseRightButtonUp" Handler="MenuItem_Click"/>
      </Style>
</ContextMenu.ItemContainerStyle>

And the code behind:

 private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        ButtonContextMenu.StaysOpen = true;
        (sender as MenuItem).ContextMenu.IsOpen = true;
    }