WPF Menu Open direction

2019-06-01 09:55发布

问题:

I have the following menu.

        <Menu>
        <MenuItem Header="Menu1">            
            <MenuItem Header="Sub1"/>
            <MenuItem Header="Sub2"/>
            <MenuItem Header="Sub3"/>
            <MenuItem Header="Sub4"/>
        </MenuItem>
        <MenuItem Header="Menu2" />
    </Menu>

The sub-menu when opened is either in an up or down direction, is there any way I can specify the direction my self?

I'd prefer the sub-menu to always render upwards since the menu it self is on the bottom of a window.

Thanks Tony

回答1:

The submenu is implemented internally using a Popup control. The default template sets the Popup's placement to Bottom which will drop the menu down if there is enough room, otherwise the menu will be "dropped up".

You can override this behavior by overriding the control template for your MenuItems. You will want to set your overridden Popup's Placement to Top.

Download the default Aero template at http://msdn.microsoft.com/en-us/library/aa972144.aspx for an example.



标签: wpf menu