It's no secret that Win32 has been an extremely successful GUI platform over the past 15 or so years. The overwhelming majority of desktop components and applications for Windows in existence today are Win32-based. So although WPF is (compared to Win32) a vastly superior platform, we did not really expect everybody to turn around a rewrite all of their existing code and applications in WPF. We knew that we had to provide a bridge between Win32 and WPF to allow easy migration between the two platforms.
There are quite a few ways in which you can use Win32 content in your WPF applications and I will cover them in several posts. This series of posts is based on the material available on the WPF/Win32 interoperability portal on MSDN.
I'll start by covering the simplest scenario of using Win32 content in a WPF application: hosting of a WinForms control in a WPF window. Since WPF currently does not have a DataGrid control out of the box, the code below will demonstrate hosting of the WinForms DataGridView control in a WPF application. A VS2005 project with the full source code is attached here.
In traditional Win32 programming user controls (buttons, listboxes, etc.) are created through a call to the CreateWindow or CreateWindowEx APIs. Every control has a unique (per session) identifier of type HWND (a handle to a Win32 window) and is recognized as a separate window (albeit a child window) by the window manager of the OS.
Windows Forms (aka WinForms) is a GUI framework, released as part of .NET 1.0, which provides a cleaner programming model around traditional Win32 that conforms to the paradigms of the .NET framework. Fundamentally, WinForms is a wrapper of Win32, so WinForms controls conform to the same WPF hosting restrictions as any other Win32 control.
Hosting of a WinForms user control in a WPF window is accomplished using the following steps:
1. In the XAML file declaring the UI of your WPF window, add a WindowsFormsHost element. As content of the WindowsFormsHost element, add the WindowsForms control you want to instantiate (DataGridView in this particular example):
Title="WinForms Control (DataGridView) in a WPF Window"
<Button Click="ButtonInsertSongOnClick">_Insert Song</Button>
<Button Click="ButtonDeleteSongOnClick">_Delete Song</Button>
2. Note how the XAML file above defines the name space for your WinForms control (the “wf” namespace) and the assembly where the WinForms class is implemented:
The directive above points to System.Windows.Forms, as that’s where the standard WinForms controls are implemented.
Note also that the DataGridView control is instantiated through XAML (see http://msdn2.microsoft.com/en-us/library/ms742875.aspx for XAML instantiation of WinForms controls). This is possible, because XAML provides a generic means of instantiating any .NET class and because of an interop technology known as Property Mapping:
The mark-up above is equivalent to the following code (see http://msdn2.microsoft.com/en-us/library/ms751761.aspx for code instantiation of WinForms controls):
public partial class MainWindow : System.Windows.Window
private void WindowLoaded(object sender, RoutedEventArgs args)
WindowsFormsHost host = new WindowsFormsHost();
DataGridView dataGridView = new DataGridView();
dataGridView.Location = new Point(0, 0);
dataGridView.ColumnsHeadersVisible = true;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.MultiSelect = false;
dataGridView.SelectionChanged += DataGridViewOnSelectionChanged;
host.child = dataGridView;
WinForms events are convenient .NET wrappers around the underlying Win32 events. Because WinForms events conform to the .NET event programming model, you can directly reuse them in your WPF application.
You attach event handlers to a WinForms control the same way you would attach event handlers to a WPF control, i.e. in XAML mark-up:
Or, in code:
Then, you provide the implementation of the event handler in code:
private void DataGridViewOnSelectionChanged(object sender, EventArgs args)
// Implementation of the event handler goes here
The WindowsFormsHost takes care of adding tab and accelerator support behind the scenes, so the hosted WinForms control is fully accessible without the need for any additional code. This is not necessarily the case when hosting unmanaged Win32 controls -- a topic which I will cover in upcoming posts.