Technically, WPF is not supported. However, you can get WPF running within a Windows Form
or a WinForms UserControl
by utilizing an ElementHost
object to hold your WPF user control.
It’s a workaround but I’ve gotten it to work in a couple of applications.
The below will host a WPF UserControl
called MainUserControl
within a windows form with MainViewModel
as its data context.
MainForm code behind:
public partial class MainForm : Form
{
private ElementHost ctrlHost;
private MainUserControl wpfControl;
public MainForm(object vault)
{
InitializeComponent();
}
private void Load_WPF_Control(object dataContext)
{
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
ctrlHost.AutoSize = true;
wpfControl = new MainUserControl(dataContext);
ctrlHost.Child = wpfControl;
this.Controls.Add(ctrlHost);
ctrlHost.AutoSize = false;
}
private void MainForm_Shown(object sender, EventArgs e)
{
MainViewModel mainViewModel = new MainViewModel();
Load_WPF_Control(mainViewModel);
}
}
WPF MainUserControl Code Behind (.xaml.cs):
public partial class MainUserControl : UserControl
{
public MainUserControl(object dataContext)
{
InitializeComponent();
DataContext = dataContext;
}
}
Disclaimer: I removed a lot of code that I don’t think is applicable for this example at least. If you have any issues getting something like this set up, please comment.