有没有办法使 ToolStripMenuItem 在菜单和工具栏之间共享?
我正在使用 Winforms 将旧的 VB6 项目移植到 C#。原始版本使用 DataDynamic 的 ActiveBar 套件来同时管理工具栏、菜单和快捷功能区中多个项目的更改。要更改在多个菜单、工具栏和上下文菜单之间共享的项目的文本,所有旧代码所要做的就是更改“工具”对象的“标题”属性。
我觉得 .NET 必须有某种方式在各种容器之间共享 ToolStripItem
,但看起来并非如此。我错过了什么吗?
目前,我已经多次开始提出各种想法,从在 ToolStripMenuItem
对象上实现 Clone
扩展方法,到让我的表单跟踪每个可共享的内容。当我意识到更新本地副本不会更新其他副本时,这失败了(我必须添加复杂的更新事件,这比简单地手动打开和关闭每个项目要工作更多)。我考虑过创建某种方法来根据其标签更新所有项目。这似乎也是不太可行的。
你们在类似情况下都使用过哪些技巧?
I'm porting an old VB6 project to C#, using Winforms. The original uses DataDynamic's ActiveBar suite to manage changes to multiple items in toolbars, menus, and shortcut ribbons simultaneously. All the old code has to do to change the text of an item shared between several menus, a toolbar, and a context menu is change the "tool" object's "Caption" property.
I feel like .NET must have some way of sharing ToolStripItem
s between a variety of containers, but it doesn't look like it does. Am I missing something?
Currently I've started several times with a variety of ideas, from implementing a Clone
extension method on ToolStripMenuItem
objects and having my form keep track of each sharable. That failed when I realized that updating the local copy wouldn't update the others (I'd have to add complex updating events that is more work than simply turning each item on and off by hand). I've considered creating some way of updating all items based on its tag. That seemed pretty infeasible as well.
What techniques have y'all used in similar situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这实际上是 .Net GUI 构建库的烦人限制之一。
常见的解决方法是动态管理菜单,在菜单打开时向其添加项目。从用户体验的角度来看,虽然丑陋,但相当无缝。
诀窍是使用以下 UI 设计范式:菜单永远不会同时显示,因此您可以热交换项目
然后使用标志和代码逻辑,您将能够知道,当菜单/工具栏/上下文菜单打开时< /em>,它应该显示哪些项目。
Well, this is actually one of the annoying limitations of .Net GUI-Building Libraries
A common workaround would be managing your Menus dynamically, adding Items to them as they are opening. Ugly but quite seamless from the user experience point of view.
The trick is to use the following UI design pradigm : Menus are never displayed simultaneously, so you can hot-swap Items
Then using flags and code logic you would be able to know, As the Menu/ToolBar/ContextMenu is opened, what Items it should display.
我最终实现了一个
SharedMenuItem
,它的职责是注册和更新需要更改的 UI 元素。上下文菜单的差异足以保证有自己的处理程序。我没有更新所有 MenuItem,而是更新了 SharedMenuItem,然后它负责其他更改。
[编辑] 代码(添加其他属性很简单/甚至可以自动生成):
I wound up implementing a
SharedMenuItem
that's responsibility was to register and update the UI elements that needed change. The context menu wound up being different enough to warrant its own handler.Instead of updating all the MenuItems I updated the
SharedMenuItem
which then took care of the other changes.[EDIT] The code (adding other properties is trivial/automatically-generatable even):