--- title: "Walkthrough: Mapping Properties Using the WindowsFormsHost Element" ms.date: 08/18/2018 dev_langs: - "csharp" - "vb" helpviewer_keywords: - "mapping properties [WPF]" - "WindowsFormsHost element property mapping [WPF]" ms.assetid: 74809167-bf8e-48b7-a2e7-b4ea08bc7d8c --- # Walkthrough: Mapping Properties Using the WindowsFormsHost Element This walkthrough shows you how to use the property to map [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] 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. For a complete code listing of the tasks illustrated in this walkthrough, see [Mapping Properties Using the WindowsFormsHost Element Sample](https://go.microsoft.com/fwlink/?LinkID=160019). When you are finished, you will be able to map [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] 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 1. Create a **WPF App** project named `PropertyMappingWithWfhSample`. 2. In **Solution Explorer**, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll. 3. In **Solution Explorer**, add references to the System.Drawing and System.Windows.Forms assemblies. ## Defining the Application Layout The [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)]-based application uses the element to host a Windows Forms control. ### To define the application layout 1. Open Window1.xaml in the WPF Designer. 2. Replace the existing code with the following code. [!code-xaml[PropertyMappingWithWfhSample#1](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml#1)] 3. Open Window1.xaml.cs in the Code Editor. 4. At the top of the file, import the following namespaces. [!code-csharp[PropertyMappingWithWfhSample#20](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#20)] [!code-vb[PropertyMappingWithWfhSample#20](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#20)] ## Defining a New Property Mapping The element provides several default property mappings. You add a new property mapping by calling the method on the element's . ### To define a new property mapping - Copy the following code into the definition for the `Window1` class. [!code-csharp[PropertyMappingWithWfhSample#14](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#14)] [!code-vb[PropertyMappingWithWfhSample#14](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#14)] The `AddClipMapping` method adds a new mapping for the property. The `OnClipChange` method translates the property to the Windows Forms property. The `Window1_SizeChanged` method handles the window's event and sizes the clipping region to fit the application window. ## Removing a Default Property Mapping Remove a default property mapping by calling the method on the element's . ### To remove a default property mapping - Copy the following code into the definition for the `Window1` class. [!code-csharp[PropertyMappingWithWfhSample#13](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#13)] [!code-vb[PropertyMappingWithWfhSample#13](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#13)] The `RemoveCursorMapping` method deletes the default mapping for the property. ## Replacing a Default Property Mapping Replace a default property mapping by removing the default mapping and calling the method on the element's . ### To replace a default property mapping - Copy the following code into the definition for the `Window1` class. [!code-csharp[PropertyMappingWithWfhSample#12](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#12)] [!code-vb[PropertyMappingWithWfhSample#12](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#12)] The `ReplaceFlowDirectionMapping` method replaces the default mapping for the property. The `OnFlowDirectionChange` method translates the property to the Windows Forms property. The `cb_CheckedChanged` method handles the event on the control. It assigns the property based on the value of the 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 `Window1` class. [!code-csharp[PropertyMappingWithWfhSample#15](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#15)] [!code-vb[PropertyMappingWithWfhSample#15](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#15)] The `ExtendBackgroundMapping` method adds a custom property translator to the existing property mapping. The `OnBackgroundChange` method assigns a specific image to the hosted control's property. The `OnBackgroundChange` method 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 event handler. ### To initialize your property mappings 1. Copy the following code into the definition for the `Window1` class. [!code-csharp[PropertyMappingWithWfhSample#11](~/samples/snippets/csharp/VS_Snippets_Wpf/PropertyMappingWithWfhSample/CSharp/PropertyMappingWithWfh/Window1.xaml.cs#11)] [!code-vb[PropertyMappingWithWfhSample#11](~/samples/snippets/visualbasic/VS_Snippets_Wpf/PropertyMappingWithWfhSample/VisualBasic/PropertyMappingWithWfh/Window1.xaml.vb#11)] The `WindowLoaded` method handles the event and performs the following initialization. - Creates a Windows Forms control. - Calls the methods you defined earlier in the walkthrough to set up the property mappings. - Assigns initial values to the mapped properties. 2. Press **F5** to build and run the application. Click the check box to see the effect of the mapping. When you click the check box, the layout reverses its left-right orientation. ## See also - - - - [Windows Forms and WPF Property Mapping](windows-forms-and-wpf-property-mapping.md) - [Design XAML in Visual Studio](/visualstudio/xaml-tools/designing-xaml-in-visual-studio) - [Walkthrough: Hosting a Windows Forms Control in WPF](walkthrough-hosting-a-windows-forms-control-in-wpf.md)