Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
WinForms and WPF: An Integrated ApproachThe concept of reuse of code with “software building blocks” has been in practice ...
   It may host one or more ActiveX controls and master/detail form with a Windows Forms       master and WPF details    ...
Upcoming SlideShare
Loading in …5
×

WinForms and WPF: An Integrated Approach

1,559 views

Published on

Integrating WinForms and WPF can help build a set of visual components into a more complex and appealing one. Read on to know how.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

WinForms and WPF: An Integrated Approach

  1. 1. WinForms and WPF: An Integrated ApproachThe concept of reuse of code with “software building blocks” has been in practice since long. Inthe .NET framework, user controls are the quintessential building blocks that give you a simpleyet 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 PresentationFoundation).WPF is an advanced technology used for rendering user interfaces in Windows-basedapplications. It was first released as part of the .NET Framework 3.0. However, WPF utilizesDirectX instead of relying on the older GDI subsystem. WinForms user controls can be built andembedded in WinForms applications, and likewise WPF user controls can be built and embeddedin WPF applications.Hybrids can be built using WinForms and WPF and then WPF controls can be embedded in aWinForms application or vice versa. Developing WPF application development is easy butmigrating existing WinForms projects to WPF involves a lot of work and effort.WPF and Windows Forms present two varied architectures for creating application interfaces. Commoninteroperation scenarios are provided by the System. The two most important classes that implementinteroperation capabilities are WindowsFormsHost and ElementHost. The latter is an easy method thatis used to integrate WinForms and WPF. It can also be used to place a WPF UIElement on theWinForms control. ElementHost enables hosting of WPF controls inside a form and on the otherhand WPF supplies the WindowsFormsHost class that allows hosting of WinForms controlsinside a WPF window.WPF and Windows Forms InteroperationBoth technologies need to have the appropriate keyboard input processing for interoperationbetween the WPF and Windows Forms to take place. A hybrid control has a control from onetechnology nested in a control from the other technology, which is known as nestedinteroperation. A multilevel hybrid control will have more than one level of hybrid controlnesting.Following are interoperation scenarios that are supported when a WPF control hosts a WinFormscontrol:  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. 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 ElementHostThe ElementHost control resides in the WindowsFormsIntegration assembly. This control isresponsible 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 WPFapplication, using a simple adapter. Due to this inherent symmetry, hybrid applications can beeasily built, mixing controls from the other platform.

×