Export (0) Print
Expand All
Expand Minimize

Walkthrough: Mapping Properties Using the WindowsFormsHost Element

This walkthrough shows you how to use the PropertyMap property to map WPF 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.

When you are finished, you will be able to map WPF properties to corresponding properties on a hosted Windows Forms control.

NoteNote:

The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu.

To complete this walkthrough you will need:

To create and set up the project

  1. Create a Windows Application (WPF) project named PropertyMappingWithWfh.

  2. In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

    The default location for this file is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

  3. In Solution Explorer, add references to the System.Drawing and System.Windows.Forms assemblies.

The WPF-based application uses the WindowsFormsHost element to host a Windows Forms control.

To define the application layout

  1. Open Window1.xaml in the XAML Code Editor.

  2. Replace the existing code with the following code.

    <Window x:Class="PropertyMappingWithWfh.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PropertyMappingWithWfh" Height="300" Width="300"
        Loaded="WindowLoaded">
      <DockPanel Name="panel1" LastChildFill="True">
        <WindowsFormsHost Name="wfHost" DockPanel.Dock="Left" SizeChanged="Window1_SizeChanged" FontSize="20" />
      </DockPanel>
    </Window>
    
    
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PropertyMappingWithWfh" Height="300" Width="300"
        Loaded="WindowLoaded">
      <DockPanel Name="panel1" LastChildFill="True">
        <WindowsFormsHost Name="wfHost" DockPanel.Dock="Left" SizeChanged="Window1_SizeChanged" FontSize="20" />
      </DockPanel>
    </Window>
    
    
  3. Open Window1.xaml.cs in the Code Editor.

  4. At the top of the file, import the following namespaces.

    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Windows.Forms.Integration;
    
    

The WindowsFormsHost element provides several default property mappings. You add a new property mapping by calling the Add method on the WindowsFormsHost element's PropertyMap.

To define a new property mapping

  • Copy the following code into the definition for the Window1 class.

    // The AddClipMapping method adds a custom 
    // mapping for the Clip property.
    private void AddClipMapping()
    {
        wfHost.PropertyMap.Add(
            "Clip",
            new PropertyTranslator(OnClipChange));
    }
    
    // The OnClipChange method assigns an elliptical clipping 
    // region to the hosted control's Region property.
    private void OnClipChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
    
        if (cb != null)
        {
            cb.Region = this.CreateClipRegion();
        }
    }
    
    // The Window1_SizeChanged method handles the window's 
    // SizeChanged event. It calls the OnClipChange method explicitly 
    // to assign a new clipping region to the hosted control.
    private void Window1_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        this.OnClipChange(wfHost, "Clip", null);
    }
    
    // The CreateClipRegion method creates a Region from an
    // elliptical GraphicsPath.
    private Region CreateClipRegion()
    {   
        GraphicsPath path = new GraphicsPath();
    
        path.StartFigure(); 
    
        path.AddEllipse(new System.Drawing.Rectangle( 
            0, 
            0, 
            (int)wfHost.ActualWidth, 
            (int)wfHost.ActualHeight ) );
    
        path.CloseFigure(); 
    
        return( new Region(path) );
    }
    
    

    The AddClipMapping method adds a new mapping for the Clip property.

    The OnClipChange method translates the Clip property to the Windows Forms Region property.

    The Window1_SizeChanged method handles the window's SizeChanged event and sizes the clipping region to fit the application window.

Remove a default property mapping by calling the Remove method on the WindowsFormsHost element's PropertyMap.

To remove a default property mapping

  • Copy the following code into the definition for the Window1 class.

    // The RemoveCursorMapping method deletes the default
    // mapping for the Cursor property.
    private void RemoveCursorMapping()
    {
        wfHost.PropertyMap.Remove("Cursor");
    }
    
    

    The RemoveCursorMapping method deletes the default mapping for the Cursor property.

Replace a default property mapping by removing the default mapping and calling the Add method on the WindowsFormsHost element's PropertyMap.

To replace a default property mapping

  • Copy the following code into the definition for the Window1 class.

    // The ReplaceFlowDirectionMapping method replaces the  
    // default mapping for the FlowDirection property.
    private void ReplaceFlowDirectionMapping()
    {
        wfHost.PropertyMap.Remove("FlowDirection");
    
        wfHost.PropertyMap.Add(
            "FlowDirection",
            new PropertyTranslator(OnFlowDirectionChange));
    }
    
    // The OnFlowDirectionChange method translates a 
    // Windows Presentation Foundation FlowDirection value 
    // to a Windows Forms RightToLeft value and assigns
    // the result to the hosted control's RightToLeft property.
    private void OnFlowDirectionChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.FlowDirection fd = (System.Windows.FlowDirection)value;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
    
        cb.RightToLeft = (fd == System.Windows.FlowDirection.RightToLeft ) ? 
            RightToLeft.Yes : RightToLeft.No;
    }
    
    // The cb_CheckedChanged method handles the hosted control's
    // CheckedChanged event. If the Checked property is true,
    // the flow direction is set to RightToLeft, otherwise it is
    // set to LeftToRight.
    private void cb_CheckedChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.CheckBox cb = sender as System.Windows.Forms.CheckBox;
    
        wfHost.FlowDirection = ( cb.CheckState == CheckState.Checked ) ? 
                System.Windows.FlowDirection.RightToLeft : 
                System.Windows.FlowDirection.LeftToRight;
    }
    
    

    The ReplaceFlowDirectionMapping method replaces the default mapping for the FlowDirection property.

    The OnFlowDirectionChange method translates the FlowDirection property to the Windows Forms RightToLeft property.

    The cb_CheckedChanged method handles the CheckedChanged event on the CheckBox control. It assigns the FlowDirection property based on the value of the CheckState property

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.

    // The ExtendBackgroundMapping method adds a property
    // translator if a mapping already exists.
    private void ExtendBackgroundMapping()
    {
        if (wfHost.PropertyMap["Background"] != null)
        {
            wfHost.PropertyMap["Background"] += new PropertyTranslator(OnBackgroundChange);
        }
    }
    
    // The OnBackgroundChange method assigns a specific image 
    // to the hosted control's BackgroundImage property.
    private void OnBackgroundChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
        ImageBrush b = value as ImageBrush;
    
        if (b != null)
        {
            cb.BackgroundImage = new System.Drawing.Bitmap(@"C:\WINDOWS\Santa Fe Stucco.bmp");
        }
    }
    
    

    The ExtendBackgroundMapping method adds a custom property translator to the existing Background property mapping.

    The OnBackgroundChange method assigns a specific image to the hosted control's BackgroundImage property. The OnBackgroundChange method is called after the default property mapping is applied.

Set up your property mappings by calling the previously described methods in the Loaded event handler.

To initialize your property mappings

  1. Copy the following code into the definition for the Window1 class.

    // The WindowLoaded method handles the Loaded event.
    // It enables Windows Forms visual styles, creates 
    // a Windows Forms checkbox control, and assigns the
    // control as the child of the WindowsFormsHost element. 
    // This method also modifies property mappings on the 
    // WindowsFormsHost element.
    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.Application.EnableVisualStyles();
    
        // Create a Windows Forms checkbox control and assign 
        // it as the WindowsFormsHost element's child.
        System.Windows.Forms.CheckBox cb = new System.Windows.Forms.CheckBox();
        cb.Text = "Windows Forms checkbox";
        cb.Dock = DockStyle.Fill;
        cb.TextAlign = ContentAlignment.MiddleCenter;
        cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
        wfHost.Child = cb;
    
        // Replace the default mapping for the FlowDirection property.
        this.ReplaceFlowDirectionMapping();
    
        // Remove the mapping for the Cursor property.
        this.RemoveCursorMapping();
    
        // Add the mapping for the Clip property.
        this.AddClipMapping();
    
        // Add another mapping for the Background property.
        this.ExtendBackgroundMapping();
    
        // Cause the OnFlowDirectionChange delegate to be called.
        wfHost.FlowDirection = System.Windows.FlowDirection.LeftToRight;
    
        // Cause the OnClipChange delegate to be called.
        wfHost.Clip = new RectangleGeometry();
    
        // Cause the OnBackgroundChange delegate to be called.
        wfHost.Background = new ImageBrush();
    }
    
    

    The WindowLoaded method handles the Loaded event and performs the following initialization.

    • Creates a 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.

  2. Press F5 to build and run the application. Click the check box to see the effect of the FlowDirection mapping. When you click the check box, the layout reverses its left-right orientation.

Community Additions

ADD
Show:
© 2014 Microsoft