Unifying ToolstripButtons and ToolstripMenuItems
Almost in all cases, ToolStripItems
do just what their equivalent MenuStripItem
d开发者_开发知识库o. 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.
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
and MenuStripItem
.
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, called IUICommand
) which provides these properties. It won't take much effort to enhance the CommandBinding
class shown in the link to set the appropriate properties in your ToolStripItem
.
精彩评论