WPF and Windows Forms Interop / Integration (AKA CrossBow)
At today's ".NET 3.0 Series" session on Windows Presentation Foundation (I was stoked by the attendance and interest levels BTW - Thanks to all that turned up) there was a lot of interest around Interop between WPF and existing Windows Forms applications.
As a follow up to this, here are some good resources and samples to get you started:
To summarise, the two key classes that implement interop are WindowsFormsHost and ElementHost
- Use the WindowsFormsHost control when you want to use a Windows Forms control on a Windows Presentation Foundation page. As in the following example XAML:
<Window x:Class="Window1"
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:wf="clr-
namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</Window>
- Use the ElementHost control when you want to use a Windows Presentation Foundation element in a Windows Forms-based application. As in the following example C#
private void Form1_Load(object sender, EventArgs e)
{
// Create the ElementHost control for hosting the
// WPF UserControl.
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
// Create the WPF UserControl.
HostingWpfUserControlInWf.UserControl1 uc =
new HostingWpfUserControlInWf.UserControl1();
// Assign the WPF UserControl to the ElementHost control's
// Child property.
host.Child = uc;
// Add the ElementHost control to the form's
// collection of child controls.
this.Controls.Add(host);
}
Hope this helps...
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using