统一 ToolstripButtons 和 ToolstripMenuItems
几乎在所有情况下,ToolStripItems
都会执行其等效的 MenuStripItem
的操作。在这些情况下,这两个命令的 ToolTip
、icon
和 text
是相同的。那么,有没有什么办法(最好是.NET原生的方式)来同步和统一这两项呢?这可能是一种在其他地方定义命令并将它们放入各种条带中的方法。
Almost in all cases, ToolStripItems
do just what their equivalent MenuStripItem
do. In these cases, ToolTip
, icon
and text
of these two commands are the same. So, is there any way (preferably a .NET native way) to synchronize and unify these two items? This might be a way to define commands somewhere else and just put them into various strips.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看 命令模式,它允许您封装所需的逻辑从
ToolStripButton
和MenuStripItem
调用。这篇 Stackoverflow 帖子提供了一些在 .net 和 我提供的这个答案展示了如何提供将命令绑定到按钮的通用方法(这可以很容易地适应其他控件,例如
ToolStripItem
)。有一个可与 WPF 一起使用的
ICommand
接口,但您没有理由不能在 Windows 窗体应用程序中使用它。最后,如果您想为命令提供其他常见属性,例如命令文本或图像,我建议从
ICommand
接口派生一个新接口(例如,称为IUICommand) 提供了这些属性。增强链接中显示的
CommandBinding
类以在ToolStripItem
中设置适当的属性并不需要花费太多精力。You should take a look at the command pattern which will allow you to encapsulate the logic that you want to call from both the
ToolStripButton
andMenuStripItem
.This Stackoverflow post provides some examples of implementing the command pattern in .net and this answer I provided shows how to provide a generic way of binding commands to buttons (this can be very easily adapted to work with other controls such as
ToolStripItem
).There is an
ICommand
interface for use with WPF but there is no reason why you cant use it in your Windows Forms application.Finally, if you want to provide other common properties for the command such as command text or an image I would suggest deriving a new interface from the
ICommand
interface (for eaxample, calledIUICommand
) which provides these properties. It won't take much effort to enhance theCommandBinding
class shown in the link to set the appropriate properties in yourToolStripItem
.