* Links: .NET Desktop - framework\wpf * Apply suggestions from code review Co-authored-by: Andy De George <[email protected]>
8.9 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| Walkthrough: Mapping Properties Using the WindowsFormsHost Element | 08/18/2018 |
|
|
74809167-bf8e-48b7-a2e7-b4ea08bc7d8c |
Walkthrough: Mapping Properties Using the WindowsFormsHost Element
This walkthrough shows you how to use the xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A property to map [!INCLUDETLA2#tla_winclient] properties to corresponding properties on a hosted Windows Forms control.
Tasks illustrated in this walkthrough include:
-
Creating the project.
-
Defining the application layout.
-
Defining a new property mapping.
-
Removing a default property mapping.
-
Replacing a default property mapping.
-
Extending a default property mapping.
When you are finished, you will be able to map [!INCLUDETLA2#tla_winclient] properties to corresponding properties on a hosted Windows Forms control.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2017
Create and set up the project
-
Create a WPF App project named
PropertyMappingWithWfhSample. -
In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.
-
In Solution Explorer, add references to the System.Drawing and System.Windows.Forms assemblies.
Defining the Application Layout
The [!INCLUDETLA2#tla_winclient]-based application uses the xref:System.Windows.Forms.Integration.WindowsFormsHost element to host a Windows Forms control.
To define the application layout
-
Open Window1.xaml in the WPF Designer.
-
Replace the existing code with the following code.
[!code-xamlPropertyMappingWithWfhSample#1]
-
Open Window1.xaml.cs in the Code Editor.
-
At the top of the file, import the following namespaces.
[!code-csharpPropertyMappingWithWfhSample#20] [!code-vbPropertyMappingWithWfhSample#20]
Defining a New Property Mapping
The xref:System.Windows.Forms.Integration.WindowsFormsHost element provides several default property mappings. You add a new property mapping by calling the xref:System.Windows.Forms.Integration.PropertyMap.Add%2A method on the xref:System.Windows.Forms.Integration.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.
To define a new property mapping
-
Copy the following code into the definition for the
Window1class.[!code-csharpPropertyMappingWithWfhSample#14] [!code-vbPropertyMappingWithWfhSample#14]
The
AddClipMappingmethod adds a new mapping for the xref:System.Windows.UIElement.Clip%2A property.The
OnClipChangemethod translates the xref:System.Windows.UIElement.Clip%2A property to the Windows Formsxref:System.Windows.Forms.Control.Region%2A property.The
Window1_SizeChangedmethod handles the window's xref:System.Windows.FrameworkElement.SizeChanged event and sizes the clipping region to fit the application window.
Removing a Default Property Mapping
Remove a default property mapping by calling the xref:System.Windows.Forms.Integration.PropertyMap.Remove%2A method on the xref:System.Windows.Forms.Integration.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.
To remove a default property mapping
-
Copy the following code into the definition for the
Window1class.[!code-csharpPropertyMappingWithWfhSample#13] [!code-vbPropertyMappingWithWfhSample#13]
The
RemoveCursorMappingmethod deletes the default mapping for the xref:System.Windows.FrameworkElement.Cursor%2A property.
Replacing a Default Property Mapping
Replace a default property mapping by removing the default mapping and calling the xref:System.Windows.Forms.Integration.PropertyMap.Add%2A method on the xref:System.Windows.Forms.Integration.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.
To replace a default property mapping
-
Copy the following code into the definition for the
Window1class.[!code-csharpPropertyMappingWithWfhSample#12] [!code-vbPropertyMappingWithWfhSample#12]
The
ReplaceFlowDirectionMappingmethod replaces the default mapping for the xref:System.Windows.FrameworkElement.FlowDirection%2A property.The
OnFlowDirectionChangemethod translates the xref:System.Windows.FrameworkElement.FlowDirection%2A property to the Windows Formsxref:System.Windows.Forms.Control.RightToLeft%2A property.The
cb_CheckedChangedmethod handles the xref:System.Windows.Forms.CheckBox.CheckedChanged event on the xref:System.Windows.Forms.CheckBox control. It assigns the xref:System.Windows.FrameworkElement.FlowDirection%2A property based on the value of the xref:System.Windows.Forms.CheckBox.CheckState%2A property
Extending a Default Property Mapping
You can use a default property mapping and also extend it with your own mapping.
To extend a default property mapping
-
Copy the following code into the definition for the
Window1class.[!code-csharpPropertyMappingWithWfhSample#15] [!code-vbPropertyMappingWithWfhSample#15]
The
ExtendBackgroundMappingmethod adds a custom property translator to the existing xref:System.Windows.Controls.Control.Background%2A property mapping.The
OnBackgroundChangemethod assigns a specific image to the hosted control's xref:System.Windows.Forms.Control.BackgroundImage%2A property. TheOnBackgroundChangemethod is called after the default property mapping is applied.
Initializing Your Property Mappings
Set up your property mappings by calling the previously described methods in the xref:System.Windows.FrameworkElement.Loaded event handler.
To initialize your property mappings
-
Copy the following code into the definition for the
Window1class.[!code-csharpPropertyMappingWithWfhSample#11] [!code-vbPropertyMappingWithWfhSample#11]
The
WindowLoadedmethod handles the xref:System.Windows.FrameworkElement.Loaded event and performs the following initialization.-
Creates a Windows Formsxref:System.Windows.Forms.CheckBox control.
-
Calls the methods you defined earlier in the walkthrough to set up the property mappings.
-
Assigns initial values to the mapped properties.
-
-
Press F5 to build and run the application. Click the check box to see the effect of the xref:System.Windows.FrameworkElement.FlowDirection%2A mapping. When you click the check box, the layout reverses its left-right orientation.
See also
- xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.Integration.ElementHost.PropertyMap%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.Integration.WindowsFormsHost
- Windows Forms and WPF Property Mapping
- Design XAML in Visual Studio
- Walkthrough: Hosting a Windows Forms Control in WPF