演练:使用 ElementHost 控件映射属性

更新:2007 年 11 月

本演练显示如何使用 PropertyMap 属性将 Windows 窗体属性映射至承载的 WPF 元素上的相应属性。

本演练涉及以下任务:

  • 创建项目。

  • 定义新的属性映射。

  • 移除默认属性映射。

  • 扩展默认属性映射。

有关本演练中演示的任务的完整代码清单,请参见使用 ElementHost 控件映射属性的示例

完成后,您将能够将 Windows 窗体属性映射至承载的元素上相应的 WPF 属性。

注意 显示的对话框和菜单命令可能与“帮助”中所述的有所不同,具体取决于当前的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

先决条件

您需要以下组件来完成本演练:

  • Visual Studio 2008.

创建项目

创建项目

  1. 创建一个名为 PropertyMappingWithElementHost 的 Windows 窗体应用程序项目。有关更多信息,请参见如何:创建 Windows 应用程序项目。 

  2. 在解决方案资源管理器中,添加对以下 WPF 程序集的引用。 

    • PresentationCore

    • PresentationFramework

    • WindowsBase

    • WindowsFormsIntegration

  3. 将下面的代码复制到 Form1 代码文件的顶部。

    Imports System.Windows
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Forms.Integration
    
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Forms.Integration;
    
  4. 在 Windows 窗体设计器中打开 Form1。双击窗体为 Load 事件添加事件处理程序。

  5. 返回至 Windows 窗体设计器,为该窗体的 Resize 事件添加事件处理程序。有关更多信息,请参见如何:使用设计器创建事件处理程序

  6. 在 Form1 类中,声明一个 ElementHost 字段。

    Private elemHost As ElementHost = Nothing
    
    ElementHost elemHost = null;
    

定义新的属性映射。

ElementHost 控件提供若干默认属性映射。您可以通过对 ElementHost 控件的 PropertyMap 调用 Add 方法来添加新的属性映射。

定义新的属性映射

  1. 将下面的代码复制到 Form1 类的定义中。

    ' The AddMarginMapping method adds a new property mapping
    ' for the Margin property.
    Private Sub AddMarginMapping()
    
        elemHost.PropertyMap.Add( _
            "Margin", _
            New PropertyTranslator(AddressOf OnMarginChange))
    
    End Sub
    
    
    ' The OnMarginChange method implements the mapping 
    ' from the Windows Forms Margin property to the
    ' Windows Presentation Foundation Margin property.
    '
    ' The provided Padding value is used to construct 
    ' a Thickness value for the hosted element's Margin
    ' property.
    Private Sub OnMarginChange( _
    ByVal h As Object, _
    ByVal propertyName As String, _
    ByVal value As Object)
    
        Dim host As ElementHost = h
        Dim p As Padding = CType(value, Padding)
        Dim wpfButton As System.Windows.Controls.Button = host.Child
    
    
        Dim t As New Thickness(p.Left, p.Top, p.Right, p.Bottom)
    
        wpfButton.Margin = t
    
    End Sub
    
    // The AddMarginMapping method adds a new property mapping
    // for the Margin property.
    private void AddMarginMapping()
    {
        elemHost.PropertyMap.Add(
            "Margin",
            new PropertyTranslator(OnMarginChange));
    }
    
    // The OnMarginChange method implements the mapping 
    // from the Windows Forms Margin property to the
    // Windows Presentation Foundation Margin property.
    //
    // The provided Padding value is used to construct 
    // a Thickness value for the hosted element's Margin
    // property.
    private void OnMarginChange(object h, String propertyName, object value)
    {
        ElementHost host = h as ElementHost;
        Padding p = (Padding)value;
        System.Windows.Controls.Button wpfButton = 
            host.Child as System.Windows.Controls.Button;
    
        Thickness t = new Thickness(p.Left, p.Top, p.Right, p.Bottom );
    
        wpfButton.Margin = t;
    }
    

    AddMarginMapping 方法为 Margin 属性添加一个新的映射。

    OnMarginChange 方法将 Margin 属性转换为 WPFMargin 属性。

  2. 将下面的代码复制到 Form1 类的定义中。

    ' The AddRegionMapping method assigns a custom 
    ' mapping for the Region property.
    Private Sub AddRegionMapping()
    
        elemHost.PropertyMap.Add( _
            "Region", _
            New PropertyTranslator(AddressOf OnRegionChange))
    
    End Sub
    
    
    ' The OnRegionChange method assigns an EllipseGeometry to
    ' the hosted element's Clip property.
    Private Sub OnRegionChange( _
    ByVal h As Object, _
    ByVal propertyName As String, _
    ByVal value As Object)
    
        Dim host As ElementHost = h
    
        Dim wpfButton As System.Windows.Controls.Button = host.Child
    
        wpfButton.Clip = New EllipseGeometry(New Rect( _
            0, _
            0, _
            wpfButton.ActualWidth, _
            wpfButton.ActualHeight))
    
    End Sub
    
    
    ' The Form1_Resize method handles the form's Resize event.
    ' It calls the OnRegionChange method explicitly to 
    ' assign a new clipping geometry to the hosted element.
    Private Sub Form1_Resize( _
    ByVal sender As Object, _
    ByVal e As EventArgs) Handles MyBase.Resize
    
        Me.OnRegionChange(elemHost, "Region", Nothing)
    
    End Sub
    
    // The AddRegionMapping method assigns a custom 
    // mapping for the Region property.
    private void AddRegionMapping()
    {
        elemHost.PropertyMap.Add(
            "Region",
            new PropertyTranslator(OnRegionChange));
    }
    
    // The OnRegionChange method assigns an EllipseGeometry to
    // the hosted element's Clip property.
    private void OnRegionChange(
        object h, 
        String propertyName, 
        object value)
    {
        ElementHost host = h as ElementHost;
        System.Windows.Controls.Button wpfButton = 
            host.Child as System.Windows.Controls.Button;
    
        wpfButton.Clip = new EllipseGeometry(new Rect(
            0, 
            0, 
            wpfButton.ActualWidth, 
            wpfButton.ActualHeight));
    }
    
    // The Form1_Resize method handles the form's Resize event.
    // It calls the OnRegionChange method explicitly to 
    // assign a new clipping geometry to the hosted element.
    private void Form1_Resize(object sender, EventArgs e)
    {
        this.OnRegionChange(elemHost, "Region", null);
    }
    

    AddRegionMapping 方法为 Region 属性添加一个新的映射。

    OnRegionChange 方法将 Region 属性转换为 WPFClip 属性。

    Form1_Resize 方法处理窗体的 Resize 事件,并调整剪辑区域的大小以适合所承载的元素。

移除默认属性映射

通过对 ElementHost 控件的 PropertyMap 调用 Remove 方法来移除默认属性映射。

移除默认属性映射

  • 将下面的代码复制到 Form1 类的定义中。

    ' The RemoveCursorMapping method deletes the default
    ' mapping for the Cursor property.
    Private Sub RemoveCursorMapping()
        elemHost.PropertyMap.Remove("Cursor")
    End Sub
    
    // The RemoveCursorMapping method deletes the default
    // mapping for the Cursor property.
    private void RemoveCursorMapping()
    {
        elemHost.PropertyMap.Remove("Cursor");
    }
    

    RemoveCursorMapping 方法删除 Cursor 属性的默认映射。

扩展默认属性映射

您可以使用默认属性映射,也可以利用自己的映射对其进行扩展。

扩展默认属性映射

  • 将下面的代码复制到 Form1 类的定义中。

    ' The ExtendBackColorMapping method adds a property
    ' translator if a mapping already exists.
    Private Sub ExtendBackColorMapping()
    
        If elemHost.PropertyMap("BackColor") IsNot Nothing Then
    
            elemHost.PropertyMap("BackColor") = PropertyTranslator.Combine( _
                elemHost.PropertyMap("BackColor"), _
                PropertyTranslator.CreateDelegate( _
                GetType(PropertyTranslator), _
                Me, _
                "OnBackColorChange"))
        End If
    
    End Sub
    
    
    ' The OnBackColorChange method assigns a specific image 
    ' to the hosted element's Background property.
    Private Sub OnBackColorChange( _
    ByVal h As Object, _
    ByVal propertyName As String, _
    ByVal value As Object)
    
        Dim host As ElementHost = h
        Dim wpfButton As System.Windows.Controls.Button = host.Child
        Dim b As New ImageBrush(New BitmapImage( _
            New Uri("file:///C:\WINDOWS\Santa Fe Stucco.bmp")))
        wpfButton.Background = b
    
    End Sub
    
    // The ExtendBackColorMapping method adds a property
    // translator if a mapping already exists.
    private void ExtendBackColorMapping()
    {
        if (elemHost.PropertyMap["BackColor"] != null)
        {
            elemHost.PropertyMap["BackColor"] += 
                new PropertyTranslator(OnBackColorChange);
        }
    }
    
    // The OnBackColorChange method assigns a specific image 
    // to the hosted element's Background property.
    private void OnBackColorChange(object h, String propertyName, object value)
    {
        ElementHost host = h as ElementHost;
        System.Windows.Controls.Button wpfButton = 
            host.Child as System.Windows.Controls.Button;
    
        ImageBrush b = new ImageBrush(new BitmapImage(
            new Uri(@"file:///C:\WINDOWS\Santa Fe Stucco.bmp")));
        wpfButton.Background = b;
    }
    

    ExtendBackColorMapping 方法将一个自定义的属性转换器添加到现有的 BackColor 属性映射中。

    OnBackColorChange 方法将一个特定的图像指定给所承载的控件的 Background 属性。在应用默认属性映射后,会调用 OnBackColorChange 方法。

初始化属性映射

初始化属性映射

  1. 将下面的代码复制到 Form1 类的定义中。

    Private Sub Form1_Load( _
    ByVal sender As Object, _
    ByVal e As EventArgs) Handles MyBase.Load
    
        ' Create the ElementHost control.
        elemHost = New ElementHost()
        elemHost.Dock = DockStyle.Fill
        Me.Controls.Add(elemHost)
    
        ' Create a Windows Presentation Foundation Button element 
        ' and assign it as the ElementHost control's child. 
        Dim wpfButton As New System.Windows.Controls.Button()
        wpfButton.Content = "Windows Presentation Foundation Button"
        elemHost.Child = wpfButton
    
        ' Map the Margin property.
        Me.AddMarginMapping()
    
        ' Remove the mapping for the Cursor property.
        Me.RemoveCursorMapping()
    
        ' Add a mapping for the Region property.
        Me.AddRegionMapping()
    
        ' Add another mapping for the BackColor property.
        Me.ExtendBackColorMapping()
    
        ' Cause the OnMarginChange delegate to be called.
        elemHost.Margin = New Padding(23, 23, 23, 23)
    
        ' Cause the OnRegionChange delegate to be called.
        elemHost.Region = New [Region]()
    
        ' Cause the OnBackColorChange delegate to be called.
        elemHost.BackColor = System.Drawing.Color.AliceBlue
    
    End Sub
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // Create the ElementHost control.
        elemHost = new ElementHost();
        elemHost.Dock = DockStyle.Fill;
        this.Controls.Add(elemHost);
    
        // Create a Windows Presentation Foundation Button element 
        // and assign it as the ElementHost control's child. 
        System.Windows.Controls.Button wpfButton = new System.Windows.Controls.Button();
        wpfButton.Content = "Windows Presentation Foundation Button";
        elemHost.Child = wpfButton;
    
        // Map the Margin property.
        this.AddMarginMapping();
    
        // Remove the mapping for the Cursor property.
        this.RemoveCursorMapping();
    
        // Add a mapping for the Region property.
        this.AddRegionMapping();
    
        // Add another mapping for the BackColor property.
        this.ExtendBackColorMapping();
    
        // Cause the OnMarginChange delegate to be called.
        elemHost.Margin = new Padding(23, 23, 23, 23);
    
        // Cause the OnRegionChange delegate to be called.
        elemHost.Region = new Region();
    
        // Cause the OnBackColorChange delegate to be called.
        elemHost.BackColor = System.Drawing.Color.AliceBlue;
    }
    

    Form1_Load 方法处理 Load 事件并执行以下初始化。

    • 创建一个 WPFButton 元素。

    • 调用在本演练前面部分定义的方法,来设置属性映射。

    • 将初始值赋值给已映射的属性

  2. 按 F5 生成并运行该应用程序。

请参见

概念

Windows 窗体和 WPF 属性映射

演练:在 Windows 窗体中承载 Windows Presentation Foundation 控件

参考

ElementHost.PropertyMap

WindowsFormsHost.PropertyMap

WindowsFormsHost

其他资源

WPF 设计器

迁移和互操作性

迁移和互操作性示例