Freigeben über


Exemplarische Vorgehensweise: Zuordnen von Eigenschaften mithilfe des ElementHost-Steuerelements

In dieser exemplarischen Vorgehensweise wird gezeigt, wie Sie die PropertyMap Eigenschaft verwenden, um Windows Forms-Eigenschaften entsprechenden Eigenschaften in einem gehosteten WPF-Element zuzuordnen.

In dieser exemplarischen Vorgehensweise werden u. a. folgende Aufgaben veranschaulicht:

  • Erstellen des Projekts.

  • Neue Eigenschaftszuordnung definieren.

  • Entfernen einer Standardeigenschaftszuordnung.

  • Erweitern einer Standardeigenschaftenzuordnung.

Wenn Sie fertig sind, können Sie Windows Forms-Eigenschaften entsprechenden WPF-Eigenschaften für ein gehostetes Element zuordnen.

Voraussetzungen

Sie benötigen die folgenden Komponenten, um diese Schritt-für-Schritt-Anleitung abzuschließen.

  • Visual Studio 2017

Projekt erstellen

So erstellen Sie das Projekt

  1. Erstellen Sie ein Windows Forms-App-Projekt mit dem Namen PropertyMappingWithElementHost.

  2. Fügen Sie im Projektmappen-Explorer Verweise auf die folgenden WPF-Assemblies hinzu.

    • PresentationCore

    • PresentationFramework

    • WindowsBase

    • WindowsFormsIntegration

  3. Kopieren Sie den folgenden Code in den Anfang der Form1 Codedatei.

    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Forms.Integration;
    
    Imports System.Windows
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Forms.Integration
    
  4. Öffnen Sie Form1 im Windows Forms-Designer. Doppelklicken Sie auf das Formular, um einen Ereignishandler für das Load Ereignis hinzuzufügen.

  5. Kehren Sie zum Windows Forms-Designer zurück, und fügen Sie einen Ereignishandler für das Ereignis des Formulars Resize hinzu. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen von Ereignishandlern mit dem Designer.

  6. Deklarieren Sie ein ElementHost Feld in der Form1 Klasse.

    ElementHost elemHost = null;
    
    Private elemHost As ElementHost = Nothing
    

Definieren neuer Eigenschaftenzuordnungen

Das ElementHost Steuerelement stellt mehrere Standardeigenschaftenzuordnungen bereit. Sie fügen eine neue Eigenschaftszuordnung hinzu, indem Sie die Add Methode für das ElementHost Steuerelement PropertyMapaufrufen.

Neue Eigenschaftenzuordnungen definieren

  1. Kopieren Sie den folgenden Code in die Definition der Form1 Klasse.

    // 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;
    }
    
    ' 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
    

    Die AddMarginMapping Methode fügt eine neue Zuordnung für die Margin Eigenschaft hinzu.

    Die OnMarginChange Methode übersetzt die Margin Eigenschaft in die WPF-Eigenschaft Margin .

  2. Kopieren Sie den folgenden Code in die Definition der Form1 Klasse.

    // 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);
    }
    
    ' 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
    

    Die AddRegionMapping Methode fügt eine neue Zuordnung für die Region Eigenschaft hinzu.

    Die OnRegionChange Methode übersetzt die Region Eigenschaft in die WPF-Eigenschaft Clip .

    Die Form1_Resize Methode behandelt das Ereignis des Formulars Resize und passt den Clippingbereich an das gehostete Element an.

Entfernen einer Standardeigenschaftszuordnung

Entfernen Sie eine Standardeigenschaftszuordnung, indem Sie die Remove Methode für das ElementHost Steuerelement PropertyMapaufrufen.

So entfernen Sie eine Standardeigenschaftszuordnung

  • Kopieren Sie den folgenden Code in die Definition der Form1 Klasse.

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

    Die RemoveCursorMapping Methode löscht die Standardzuordnung für die Cursor Eigenschaft.

Erweitern einer Standardeigenschaftszuordnung

Sie können eine Standardeigenschaftszuordnung verwenden und sie auch mit Ihrer eigenen Zuordnung erweitern.

So erweitern Sie eine Standardeigenschaftszuordnung

  • Kopieren Sie den folgenden Code in die Definition der Form1 Klasse.

    // 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;
    }
    
    ' 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
    

    Die ExtendBackColorMapping Methode fügt der vorhandenen BackColor Eigenschaftszuordnung einen benutzerdefinierten Eigenschaftenübersetzer hinzu.

    Die OnBackColorChange Methode weist der Eigenschaft des gehosteten Steuerelements Background ein bestimmtes Bild zu. Die OnBackColorChange Methode wird aufgerufen, nachdem die Standardeigenschaftszuordnung angewendet wurde.

Eigenschaftenzuordnungen initialisieren

  1. Kopieren Sie den folgenden Code in die Definition der Form1 Klasse.

    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;
    }
    
    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
    

    Die Form1_Load Methode behandelt das Load Ereignis und führt die folgende Initialisierung aus.

    • Erstellt ein WPF-Element Button .

    • Ruft die Methoden auf, die Sie weiter oben in der exemplarischen Vorgehensweise definiert haben, um die Eigenschaftenzuordnungen einzurichten.

    • Weist den zugeordneten Eigenschaften Anfangswerte zu.

  2. Drücken Sie F5, um die Anwendung zu erstellen und auszuführen.

Siehe auch