在wpf中对齐菜单
我在窗口左侧设置了一个垂直菜单。它的项目就在其上方(上方)打开,这会阻止用户在项目打开时获得菜单的完整视图。
我希望每个元素都在菜单的右侧打开,以便获得菜单其余部分和打开的元素的完整视图。这怎么能做到呢?可以借助变换或触发器吗?
这是一些代码:
<MenuItem Header="Maths">
<MenuItem Background="LightGray" Header="Add"/>
<MenuItem Background="LightGray" Header="Subtract"/>
<MenuItem Background="LightGray" Header="Multiply"/>
<MenuItem Background="LightGray" Header="Divide"/>
</MenuItem>
I have a vertical menu set to the left side of the window. Its items open just on (above) it and this prevents the user from having a full view of the menu when an item is open.
I want each element to open just on the right of the menu, so as to have an entire view of both the rest of menu and the opened elements. How can this be done? May be with the aid of transforms or triggers?
Here is some code:
<MenuItem Header="Maths">
<MenuItem Background="LightGray" Header="Add"/>
<MenuItem Background="LightGray" Header="Subtract"/>
<MenuItem Background="LightGray" Header="Multiply"/>
<MenuItem Background="LightGray" Header="Divide"/>
</MenuItem>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,需要明确的是,上面的 MenuItem“数学”位于 WPF 菜单中,并且您已将该菜单的 ItemsPanel 更改为垂直 StackPanel 或其他内容,因此“数学”位于其他同级菜单项的上方/下方。如果是这样,则发生的情况是,角色为 TopLevelHeader(具有子项且直接位于菜单内的 MenuItem)的 MenuItem 的默认模板使得弹出窗口位于菜单项下方(或上方)。您可能想要重新模板化这些菜单项。黑客(和丑陋的替代方案)是使用将用于 SubmenuHeader 角色菜单项的模板(即具有子项且位于另一个 MenuItem 内的 MenuItem)。例如
So just to be clear, the MenuItem 'Maths' above is in a WPF Menu and you have changed that Menu's ItemsPanel to be a Vertical StackPanel or something so 'Maths' is above/below other sibling menu items. If so what is happening is that the default template for MenuItem's whose role is TopLevelHeader (MenuItem that has child Items and is directly within a Menu) is such that the popup is below (or above) the menu item. You will probably want to retemplate those menu items. On hacky (and ugly alternative) is to use the template that would be used for SubmenuHeader role menu items (i.e. a MenuItem that has child Items and is within another MenuItem). e.g.