如何实现“超级菜单”在 WPF 中?
我正在尝试使用 WPF 实现“Mega Menu”样式菜单。要查看网页设计中的大型菜单示例,请参阅此处。
到目前为止,我已经尝试通过使用 TextBlocks 作为菜单的最高级别来创建类似的界面,然后使用鼠标悬停事件来显示位于文本块下方的附加窗口。这是麻烦且不灵活的,将来的更改将需要动态添加/删除 TextBlock。
我考虑过使用 WPF 菜单控件,因为我知道样式可以进行显着修改,但我还没有看到任何方法可以使用菜单控件使用的分层模型生成多列布局。
有更好的方法吗?我是否必须坚持使用自定义窗口和相对定位?有人可以给我举一个已经实施的例子吗?
I'm trying to implement "Mega Menu" style menus using WPF. To see examples of mega menus in web design, see here.
So far, I've tried creating a similar interface by using TextBlocks as the highest level of the menu, and then using the mouse hover event to display an additional window that appears positioned below the text block. This is cumbersome and inflexible, future changes would require adding/removing TextBlocks dynamically.
I have considered using the WPF Menu control, because I know the styles can be dramatically modified, but I haven't seen any way to produce multi-column layouts with the hierarchical model that the Menu control uses.
Is there a better way to do this? Am I going to have to stick with custom windows and relative positioning? Can someone point me to an example of this that has already been implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Popup 控件,而不是使用自定义窗口和定位。您可以使用
StaysOpen=false
设置在用户单击屏幕外时将其关闭。如果您可以满足于单击菜单项而不是悬停,则以下自定义控件将起作用:
您需要一个样式来显示它 - 类似这样。请注意 PART_ 模板部分名称:
菜单的 XAML 将是:
让菜单在悬停时显示要困难得多,因为弹出窗口会窃取焦点(您可以显示菜单,但如果它们出现,则无法轻松隐藏它)将鼠标悬停在另一个菜单上)。为此,自定义窗口可能会更好。
Instead of using custom Windows and positioning, you could use a Popup control. Your can use the
StaysOpen=false
setting to have it close when the user clicks off-screen.If you can settle for clicking a menu item instead of hovering, the following custom control will work:
You would need a style to display it - something like this. Note the PART_ template part names:
The XAML for your menu would then be:
Making the menu appear on hover is much harder, because of the way Popups steal focus (you can show the menu, but you can't easily hide it if they mouse over another menu). For that a custom window might work better.
您可以使用 HeaderedItemsControl 并更换
Panel
以满足您的需求;默认情况下,它使用StackPanel
但WrapPanel
可能更适合您。弹出和鼠标悬停行为默认情况下不存在,需要实现。更多 稳健的方法是利用自定义的扩展器;因为它提供了您想要的弹出行为,并且链接到演练提供了鼠标悬停行为。
You could use a HeaderedItemsControl and swap out the
Panel
to suit your needs; by default it uses aStackPanel
however aWrapPanel
may suit you better. The pop out and mouse over behavior do not exist by default and would need to be implemented.A more robust approach would be to leverage a custom Expander; as it provides the pop out behavior you are after and the linked to walkthrough provides the mouse over behavior.
我想知道是否可以改装 Ribbon 控件来做到这一点?它提供选项卡、标签、列等等。
请谨慎使用此 UI 设计,并确保它仅在用户明确请求时打开和关闭。当我正在查看的网站上出现弹出式巨型菜单时,我无法将其关闭,除非我想单击它,然后它就会消失,这非常烦人。
I wonder if the Ribbon control can be retrofitted to do this? It provides tabs, labels, columns and all that.
Please use this UI design sparingly and make sure that it only opens and closes when the user specifically requests such. It's tremendously annoying when a popup mega-menu appears over a website I'm viewing, and I can't get it to close, except for when I want to click on it and it goes away.
自定义窗口和相对位置本质上是 WPF Menu/MenuItem 控件的工作方式...但正如您所发现的,它并不简单。最好的办法是重新模板化 Menu/MenuItem 控件以满足您的需求。
Custom windows and relative position are essentially how the WPF Menu/MenuItem control works... but as you've found, it's non-trivial. Best bet would be to retemplate the Menu/MenuItem controls to meet your need.