Hosting a WPF control in a Windows Form Application


In case you are trying to Host a WPF control in a Windows Form Application the sample in the SDK isn’t rendering due to the fact it attempting to put the control in a panel.  The sample has also changed the default control from a page to Grid – this is required! (see XAML below). 


In either case i thought i would post a simple sample.  


Note the references the live in: C:\Program Files\Reference Assemblies\Microsoft\WinFx\v3.0


 


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Windows.Controls;


using System.Windows.Forms.Integration;


using System.Windows.Media;


 


namespace WindowsApplication1


{ public partial class Form1 : Form


{


private ElementHost ctrlHost;


private MyControls.Page1 avAddressCtrl;


private MyControls.Page2 avAddressCtrl2;


public Form1()


{


InitializeComponent();


}


private void Form1_Load(object sender, EventArgs e)


{


ctrlHost = new ElementHost();


ctrlHost.Dock = DockStyle.Fill;


avAddressCtrl = new MyControls.Page1();


avAddressCtrl2 = new MyControls.Page2();


avAddressCtrl2.InitializeComponent();


ctrlHost.Child = avAddressCtrl2;


this.Controls.Add(ctrlHost);


}


}


}


 


<Grid x:Class=MyControls.Page2


xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation


xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Height=121 Width=111>


<Button Height=21.96 Margin=10,10,32,0 Name=button1 VerticalAlignment=Top>Button</Button>


</Grid>


Comments (3)

  1. On the Microsoft forums I saw the question: Can someone suggest me how shuld I use .NET controls in Windows

  2. On the Microsoft forums I saw the question: Can someone suggest me how shuld I use .NET controls in Windows