Instead”of rewriting your user interface completely from scratch for WPF, you can use existing Windows Forms controls within WPF applications, and create new WPF controls to be used within Windows Forms applications. The best way of integrating Wllldows Forms and WPF is by creating controls and integrating the controls in the application types of the other technology.
the integration of Windows “forms Qnd WPF Iuls a big drawback. If you inttgTQte Windows Forms with WPF, the Windows Forms controls still look like they looked in the old days. Windows Forms controls and applialtions don’t gd the nmI look of WPF. From a user interfoet stll1Ulpoint, it would be better to rewrite the UI campletely.
WPF Controls Within Windows Forms
You can use WPF controls within a Windows Forms application. A WPF element is a normal .NET class. However, you cannot use it directly from the Windows Forms rode; a WPF control is not a Windows Forms control. The integration can be done by the wrapper class ElementHost from the namespace System. Windows.Forms. Integration. ElementHost is a Wllldows Forms control, because it derives from System. Windows.Forms .Control, and can be used like any other Wllldows Forms control in a Wllldows Forms application. El~entHost hosts and manages WPF controls.
Let’s start with a simple WPFcontrol. With Visual Studio 2008,you can create a WPF User Control Library. The sample control is derived from the base class UserControl and contains a grid and a
button with a custom content:
You can create a Windows Forms application by selecting the Windows Forms Application template. Because the WPF user control project is in the same solution as the Windows Forms application, you can drag and drop the WPF user control from the toolbox to the designer surface ofthe WiFldowsForms application. This adds references to the assemblies PresentationCore, PresentationFramework, WindowsBase, WindowsFormslntegration, and of course, the assembly containing the WPF control.
Within the designer-generated code you will find a variable referencing the WPF user control and an object of type ElementHost that wraps the control:
private System.Windows.Forms.lntegration.ElementHost elementHost1;
private WPFControl.UserContro11 userContro111;
In the method InitializeComponent you can see object initializations and the assigning of the WPF control instance to the Child property of the ElementHost class:
Starting the Windows Forms application, you can see both the WPF control as well the Windows Forms . control inside one form, as .shown
Of course, you can add methods, properties, and events to the WPF control and use them the same way as other controls.
Windows Forms Controls Within WPF Applications
You can integrate Windows Forms and WPF in the other direction as well by placing a Windows Forms control within a WPF application. As with the ElementHost class used to host a WPF control inside Windows Forms, now you need a wrapper that is a WPF control to host a Windows Forms control. This class has the name WindowsFormsHost and is in the same assembly, WindowsFormslntegration. The class WindowsFormsHost is derived from the base classes HwndHost and FrameworkElement, and thus can be used as a WPF element.
For this integration, a Windows Control Library is created first. Add a TextBox and Button control to the form by using the Designer. To change the Text property of the button, the property ButtonText is added to the code behind:
In the WPF application, you can add a windowsFormsHost object from the toolbox to the Designer. This adds a reference to the assemblies WindowsF’ormslntegration, System. Windows. Forms, and the assembly of the Windows Forms control. To use the Windows Forms control from XAML, you must add an XML namespace alias to reference the .NET namespace. Because the assembly containing the Windows Forms control is in a different assembly than the WPF application, you also must add the assembly name to the namespace alias. The Windows Forms control can now be contained within the WindowsFormsHost element as shown. You can assign a value for the property ButtonText directly from XAML similarly to .NET Framework’ elements.
You can see a view of the WPF application in Figure 35-29. Of course, the Windows Forms control still looks like a Windows Forms control and does not have all the resizing and styling features you get with WPF.