Tuesday, October 19, 2010

Host WPF Controls in Windows Forms

To host a WPF control or WPF user control (wpfControl in this example) into Windows Form application, create an instance of ElementHost Class and add your WPF control/user control as child to the instance of ElementHost.

using System.Windows.Forms.Integration; 
//Assembly:   WindowsFormsIntegration (in WindowsFormsIntegration.dll)

ElementHost elementHost = new ElementHost();
elementHost.Dock = DockStyle.None;
elementHost.Child = wpfControl;

Now, add the instance of ElementHost to a container control in your windows form (for instance containerPanel here)

containerPanel.Controls.Add(elementHost);

No comments:

Post a Comment