RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Enable WPF/Windows Forms Interoperability with WPF Commands : Page 2

Windows Presentation Foundation (WPF) can interoperate with Windows Forms, letting you add legacy controls to your new interface—or new WPF controls to your existing Windows Forms applications. Either way, you need to make them play nicely together.


Hosting WPF Controls in Windows Forms
Microsoft followed a similar pattern to allow Windows Forms to host WPF controls. The Windows Forms counterpart to the WindowsFormsHost is called ElementHost. In Visual Studio 2008, you'll find it on the WPF Interoperability tab of the Toolbox. Drag this control onto a Windows Form and then write the following code:

   System.Windows.Controls.TextBox textBox = new 
      textBox.Text = "WPF TextBox";
      elementHost1.Child = textBox;
Figure 2. Windows Forms Hosting WPF Control: This Windows Form hosts a WPF TextBox control by adding it as a child of an ElementHost control.

The resulting window at runtime should look something like Figure 2.

As you can see, it's relatively easy to add controls of either type to a window of either type, but to interact with the hosted controls, you also need to be able to send commands and receive event messages.

Executing WPF Commands from Windows Forms
The key to executing commands is by interacting with the ICommand interface. Note that WPF commands are singleton objects that implement the ICommand interface, shown below:

   public interface ICommand
       event EventHandler CanExecuteChanged;
       bool CanExecute(object parameter);
       void Execute(object parameter);

The ICommand interface is relatively self explanatory. To execute a command, invoke Execute. You can find out whether a specific command can execute or not via the CanExecute method. Finally, as its name suggests, the CanExecuteChanged event gets fired whenever the value returned by CanExecute changes.

To illustrate these features, hook up a Windows Forms menu to the ApplicationCommands.Paste command. The goal is to associate the Enabled property of the menu with the result from CanExecute; in other words, the menu item should be enabled when CanExecute is true and disabled when it's false. To cause the paste to occur, invoke the Execute method of the Paste command when a user clicks the menu item.

Here's a class called MenuCommandBridge that brings all this functionality together. The class has the following properties:

   public ICommand Command { get; private set; }
   public ToolStripMenuItem MenuItem { get; private set; }
   public Object CommandParameter { get; private set; }

Set the Command property to the command that you want to hook the menu up to (ApplicationCommands.Paste in this case). Set the MenuItem property to the MenuItem that invokes the command. The CommandParameter property is an optional value that you can pass to the ICommand.Execute and ICommand.CanExecute methods.

The following event handler code invokes the command:

   private void MenuItemClicked(object sender, EventArgs e)

When the CanExecute state of the command changes, this code updates the UI:

   private void CanExecuteChanged(object sender, EventArgs e)
      this.MenuItem.Enabled = Command.CanExecute(

To hook up a menu item to the Paste command, this line of code goes into the constructor of the form (after the InitializeComponent call):

   MenuCommandBridge commandBridge = 
      new MenuCommandBridge(
      menuPaste, null)
Author's Note: You must hold a reference to the MenuCommandBridge for the lifetime of the menu. An easy way to do this is to store it in a collection as a member of the form.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date