Welcome to MSDN Blogs Sign in | Join | Help

Graham Elliott

Microsoft Services Architect

Syndication

News

My Photos
This is a Flickr badge showing public photos from Graham Elliott tagged with badge. Make your own badge here.

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...

Published Thursday, September 14, 2006 4:47 PM by grahame

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 RSS

Comments

# Cell Processor;.NET 3.0 Slides and White Papers; CrossBow &laquo; Tales from a Trading Desk @ Thursday, September 14, 2006 5:04 PM

PingBack from http://mdavey.wordpress.com/2006/09/14/cell-processornet-30-slides-and-white-papers-crossbow/

Cell Processor;.NET 3.0 Slides and White Papers; CrossBow « Tales from a Trading Desk

# re: WPF and Windows Forms Interop / Integration (AKA CrossBow) @ Friday, December 07, 2007 2:58 PM

that won't work.

Error Value of type 'HostingWpfUserControlInWf.UserControl1' cannot be converted to 'System.Windows.UIElement'.

marcelo

# re: WPF and Windows Forms Interop / Integration (AKA CrossBow) @ Saturday, June 13, 2009 8:16 AM

話題の小向美奈子ストリップを隠し撮り!入念なボディチェックをすり抜けて超小型カメラで撮影した神動画がアップ中!期間限定配信の衝撃的映像を見逃すな

小向美奈子

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker