Managing commands

Handling commands through menu or toolbar event in .Net is easy: you click on the item and choose the handler for the Click event. If two items have the same purpose (the FileOpen menu item and the Open button for instance), you simply use the same handler.

Managing the state of the command is trickier as you cannot bind the properties of two ToolStripItem because it is not a BindableComponent. As most of the properties have a Changed event, one easy way would be to implement a ToolStripItemBinder component extender adding a BindTo property. It could to fine for simple user controls where you want to provide a ToolStrip and a ContextMenuStrip to manipulate the data on a ListView for instance.

Nevertheless, command management at the application level might need better abstraction than this.

The UnifiedCommandManager sample show how to add command to the command manager and then extend the ToolStripItem to attach a command. But I was not able to find how to register on the Execute event at design time and I find it entangled to the ToolStripItem. I also really want to have support for the IMenuCommandService. I like to base interaction between my component on interfaces from the framework. I find that it lessen the coupling. But not only the MenuCommand class is named too restrictively (UICommand would have been good) but the CommandID and the EventHandler on the constructor makes it difficult to wrap it in a Component.

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.