WPF Integration in Windows Forms
Hosting WPF control in Windows form:
Windows Presentation Foundation (WPF) provides rich Ui compared to WindowsForms.These features of WPf can be used in the windows forms.We can embed the controls developed in WPF into Wnforms.
This is achieved using the Elementhost.
Namespace: System.Windows.Forms.Integration
Assembly: WindowsFormsIntegration
BBelow is the sample where a WPF Expander control is used in windows forms.
Steps:
1.Create a WindowsForm project HostWPFControl.
2.Add the following references to the project
PresentationFramework
PresentationCore
WindowsBase
WindowsFormsIntegration
3.Open the Form1.cs. file in designer.
4.Open the Form1.cs file, add the following reference.
using System.Windows.Forms.Integration;
5.Add the Form1_Load event, add the following code.
private ElementHost WPFHost;
private void Form1_Load(object sender, EventArgs e)
{
Expander expander = new Expander();
expander.Header = "Sample";
WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
WindowsFormsHost host = new WindowsFormsHost();
host.Child = new DateTimePicker();
expander.Content = host;
WPFHost.Child = expander;
this.Controls.Add(WPFHost);
expander.Expanded += new System.Windows.RoutedEventHandler(onExpand);
expander.Collapsed += new System.Windows.RoutedEventHandler(onCollapse);
}
private void onExpand(object sender, System.Windows.RoutedEventArgs e)
{
}
private void onCollapse(object sender, System.Windows.RoutedEventArgs e)
{
}
6.Now run the application.You will see the expander control, when expanded you see a datetimepicker.