共用方式為


逐步解說:使用 ElementHost 控制項對應屬性

本逐步解說顯示如何使用 PropertyMap 屬性將 Windows Forms 屬性對應至裝載之 WPF 項目上的對應屬性。

逐步解說將說明的工作包括:

  • 建立專案。

  • 定義新的屬性對應。

  • 移除預設的屬性對應。

  • 擴充預設的屬性對應。

如需這個逐步解說中所說明之工作的完整程式碼清單,請參閱使用 ElementHost 控制項對應屬性範例 (英文)。

完成時,就可以將 Windows Forms 屬性對應至裝載之項目上的對應 WPF 屬性。

必要條件

您需要下列元件才能完成此逐步解說:

  • Visual Studio 2008。

建立專案

若要建立專案

  1. 建立 Windows Forms 應用程式專案,取名為 PropertyMappingWithElementHost。 如需詳細資訊,請參閱 HOW TO:建立新的 Windows Form 應用程式專案

  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 Form 設計工具中開啟表單 Form1。 按兩下表單加入 Load 事件的事件處理常式。

  5. 回到 [Windows Form 設計工具],並加入表單的 Resize 事件的事件處理常式。 如需詳細資訊,請參閱 HOW TO:使用設計工具建立事件處理常式

  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 屬性轉譯成 WPF Margin 屬性。

  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
    
        If elemHost IsNot Nothing Then
            Me.OnRegionChange(elemHost, "Region", Nothing)
        End If
    
    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 屬性轉譯成 WPF Clip 屬性。

    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 事件並執行下列初始化。

    • 建立 WPF Button 項目。

    • 呼叫您稍早在這個逐步解說中定義的方法,以設定屬性對應。

    • 將初始值指派給對應的屬性。

  2. 按 F5 建置並執行應用程式。

請參閱

參考

ElementHost.PropertyMap

WindowsFormsHost.PropertyMap

WindowsFormsHost

概念

Windows Form 和 WPF 屬性對應

逐步解說:在 Windows Form 中裝載 WPF 複合控制項

其他資源

WPF 設計工具