Professional VB 2005 with .NET 3.0 (Programmer to Programmer)

Crossbow was the code name for the project to provide a library that enables WPF applications to host Windows Forms controls and vice versa. The Crossbow project’s focus was to provide a .NET library that developers could leverage; what it created was the WindowsFormsIntegration library. WindowFormsIntegration.dll supports the Windows.Forms.Integration namespace. This namespace provides the tools necessary for using WPF and Windows Forms in a single application. At the core of this namespace are the two classes ElementHost and WindowsFormHost. These two classes provide for interoperability in the WPF and the Windows Forms environment, respectively.

The place to start, then, is with the location of this library. Its installation location isn’t where I expected, with the .NET 3.0 libraries. Instead, when you install the .NET 3.0 Framework, this library is placed with some other libraries into a directory under Program Files. The full path is: C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

The next step in looking at this library is to review a list of the classes and the delegate that make up the Windows.Forms.Integration namespace:

Open table as spreadsheet

Class

Description

ChildChangedEventArgs

This class is used when passing event arguments to the ChildChanged event. This event occurs on both the WindowsFormsHost and ElementHost classes when the content of the Child property is changed.

ElementHost

This is the core class for embedding WPF controls within Windows Forms. Using the Child property, you identify the top-level object (probably some type of panel) that will be hosted, and via this object define an area that will be controlled by that object. The object referenced by the host can contain other controls, but the host references only this one.

IntegrationExceptionEventArgs

This is the base class for the Integration and Property Mapping exception classes. It provides the common implementation used by these classes.

LayoutExceptionEventArgs

This class enables you to return information related to a Layout error within a host class to the hosting environment, Windows Forms, or WPF.

PropertyMap

A property on each of the host classes. It provides a way for a Windows Form to tell a nested ElementHost that the size of the containing form has changed. It works the same for WPF applications hosting a WindowsFormHost control and it supports most control properties.

PropertyMappingExceptionEventArgs

Similar to the layout exception class, this enables a hosted control to return information related to an exception to the hosting environment.

WindowsFormsHost

This is the primary control when a WPF application wants to host Windows Forms controls. Similar to the ElementHost, the actual WIndowsFormsHost object contains only a single child - typically a user control. This control can then contain an array of controls, but it is this class which acts as the virtual Windows Form that is referencing the user control.

PropertyTranslator

This is the only delegate in this namespace. It is used within your Visual Basic code to enable you to translate properties from a WindowsFormsHost control to a WPF Element Host control (and vice versa). Essentially, you provide it with the property to be updated and the value to update that property with, and this method passes that value across the boundary from one UI model to the other. It works in conjunction with the PropertyMap class.

These classes enable your application to host controls within its display area. As noted, when you add the appropriate host class to your display area, it contains a control. This element contains only a single control in order to allow the integration library to assign this control the display area allocated to the host. Thus, when you assign a control to a WindowsFormsHost, behind the scenes the Margin, Docking, AutoSizing, and Location properties of the WindowsFormsHost control are automatically applied to the Child control. These properties are then controlled via the host for your outermost control.

You might still want to control the layout characteristics of elements within the control hosted by the WPF, but you should think of the top-level control, which is the child, as being controlled by the host object. Similarly, the ElementHost control automatically controls the display characteristics associated, including the following properties: Height, Width, Margin, HorizontalAlignment, and VerticalAlignment. In both cases, the host control acts as the virtual display area for the hosted control, and you should manage that display area via the host control, not the child it contains. Even though both controls are targeted at area controls such as user controls and panels, their purpose is to access controls and features across the UI display models.

Категории