January 22, 2010

Hosting WPF Controls In Windows Forms

Host WPF controls in a windows form control (using an ElementHost)
WPF for those who know Windows Forms (Large document)

You must add references to "WindowsBase", "WindowsFormsIntergration", "PresentationCore" and "PresentationFramework"
private ElementHost wpfCtrlHost;
private TestWpfControl testWpfCtrl;

public WindowsFormHost()
{
    InitializeComponent();
 ...
    HostWpfControl();
}

private void HostWpfControl()
{
    wpfCtrlHost = new ElementHost();
    wpfCtrlHost.Dock = DockStyle.Fill;
    this.Controls.Add(wpfCtrlHost);
    testWpfCtrl = new TestWpfControl();
    testWpfCtrl.InitializeComponent();
    wpfCtrlHost.Child = testWpfCtrl;
}

No comments: