1. WinForms and WPF: An Integrated Approach
The concept of reuse of code with “software building blocks” has been in practice since long. In
the .NET framework, user controls are the quintessential building blocks that give you a simple
yet powerful way to build a set of visual components into a more complex and appealing one.
Another way of doing this would be by using WinForms and WPF (Windows Presentation
Foundation).
WPF is an advanced technology used for rendering user interfaces in Windows-based
applications. It was first released as part of the .NET Framework 3.0. However, WPF utilizes
DirectX instead of relying on the older GDI subsystem. WinForms user controls can be built and
embedded in WinForms applications, and likewise WPF user controls can be built and embedded
in WPF applications.
Hybrids can be built using WinForms and WPF and then WPF controls can be embedded in a
WinForms application or vice versa. Developing WPF application development is easy but
migrating existing WinForms projects to WPF involves a lot of work and effort.
WPF and Windows Forms present two varied architectures for creating application interfaces. Common
interoperation scenarios are provided by the System. The two most important classes that implement
interoperation capabilities are WindowsFormsHost and ElementHost. The latter is an easy method that
is used to integrate WinForms and WPF. It can also be used to place a WPF UIElement on the
WinForms control. ElementHost enables hosting of WPF controls inside a form and on the other
hand WPF supplies the WindowsFormsHost class that allows hosting of WinForms controls
inside a WPF window.
WPF and Windows Forms Interoperation
Both technologies need to have the appropriate keyboard input processing for interoperation
between the WPF and Windows Forms to take place. A hybrid control has a control from one
technology nested in a control from the other technology, which is known as nested
interoperation. A multilevel hybrid control will have more than one level of hybrid control
nesting.
Following are interoperation scenarios that are supported when a WPF control hosts a WinForms
control:
The WPF can host one or more Windows Forms controls using code.
It may control one or more Windows Forms controls using XAML.
It may host a master/detail form with a WPF master and Windows Forms details.
It may host Windows Forms container controls that contain other Windows Forms
controls.
2. It may host one or more ActiveX controls and master/detail form with a Windows Forms
master and WPF details
It may host hybrid controls using code or XAML.
Integrating WinForms and WPF using ElementHost
The ElementHost control resides in the WindowsFormsIntegration assembly. This control is
responsible for loading the WPF control and painting it. Following is a step-by-step guide:
1. First create a user control in WPF by creating a StackPanel inside the grid with a
TextBlock and a Button. In order to view a WPF Event, hookup an event handler to the
Button.
2. Create a WinForm Host for this user control and then create a new Winform project and
add a reference to the dlls- PresentationCore.dll, PresentationFramework.dll,
WindowsBase.dll, WindowsFormsIntegration.dll and the WPF DLL which is already
created.
3. To view interoperability between WPF and WinForms, drag a WinForms label and
button and hook it to an event handler.
4. In the Form Load handler, create an ElementHost. Since the WPF control’s assembly
reference is already added, creation an object of the WPF user control can be done.
5. The control cannot be added to the Form’s control collection since the Form object has
no idea how to draw the WPF control. To do this, make use of the ElementHost control
that takes in the user control as its child control and adds it to the WinForm.
WPF controls can be embedded in a WinForms application or WinForms controls in a WPF
application, using a simple adapter. Due to this inherent symmetry, hybrid applications can be
easily built, mixing controls from the other platform.