Alex Rivera | Logout

WPF: Nested MenuItems in a ToolBar

Asked 2009-04-10T02:17:09.023
9

I am working on some XAML for a wpf application and I am having some trouble getting it to do what I want. Here is a sample of my XAML:

<!-- Tool Bar Tray -->
<ToolBarTray Name="toolBarTray1" DockPanel.Dock="Top">
    <!-- File And Edit Tools -->
    <ToolBar Name="toolBar1" Band="1" BandIndex="1">
        <!-- Regular Items -->
        <Button>A</Button>
        <Button>B</Button>
        <!-- Overflow Menu For Special Items -->
        <MenuItem ToolBar.OverflowMode="Always" Header="Special Items">
            <MenuItem Header="C"/>
            <MenuItem Header="D"/>
        </MenuItem>
    </ToolBar>
</ToolBarTray>

When I click on the overflow button of my toolbar, the "Special Items" MenuItem appears with a little arrow next to it, indicating nested elements. However, when I hover the mouse over "Special Items" or try to click on it, the MenuItems "C" and "D" are not being displayed.

I was hoping that MenuItem would just work outside of a Menu, but I tried to do the straight-forward thing, just in case. Including these MenuItems inside a Menu and, instead, giving this Menu the ToolBar.OverflowMode="Always" property produces some unwanted styling. The arrow is no longer present, the "Special Items" entry needs to be clicked on to activate the sub-menu and the sub-menu positioning looks a little off.

Does anyone know what is going on?

Edit: Adding a menu to the overflow is producing exactly what I requested (big surprise). What I am after is a way to convert top-level headers and items to the sub-menu level. I have turned towards this control template example on MSDN for a solution (below).

Edit,Edit: @gcores (comment discussion): Really? Am I missing something?

<ToolBar Name="toolBar1" Band=
Edit
Report

1 Answer

0

The only way I could find to even come close to generating this behaviour was to create a menu in the overflow which contained a single menu item whose header was itself another menu item called "Special Items" and containing the proper children. It worked as intended but looked bizarre (this could be remedied by custom templates) and also seems like a huge hack. The only "proper" way to do this that I can think of would be making your own MenuItem-like control which pops up a ContextMenu or Popup when hovered upon, since I don't think that a custom ControlTemplate can change the default behaviour of a menu so as not to require a click on the top level item.

answered 2009-04-12T01:48:21.087

Your Answer