Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Diese Anleitung zeigt Ihnen, wie Sie die PropertyMap-Eigenschaft verwenden, um WPF-Eigenschaften den entsprechenden Eigenschaften eines Windows Forms-Steuerelements, das gehostet wird, zuzuordnen.
In dieser exemplarischen Vorgehensweise werden u. a. folgende Aufgaben veranschaulicht:
Erstellen des Projekts.
Definieren des Anwendungslayouts.
Neue Eigenschaftszuordnung definieren.
Entfernen einer Standardeigenschaftszuordnung.
Ersetzen einer Standardeigenschaftszuordnung.
Erweitern einer Standardeigenschaftenzuordnung.
Wenn Sie fertig sind, können Sie WPF-Eigenschaften entsprechenden Eigenschaften in einem gehosteten Windows Forms-Steuerelement zuordnen.
Voraussetzungen
Sie benötigen die folgenden Komponenten, um diese Schritt-für-Schritt-Anleitung abzuschließen.
- Visual Studio 2017
Erstellen und Einrichten des Projekts
Erstellen Sie ein WPF-App-Projekt mit dem Namen
PropertyMappingWithWfhSample.Fügen Sie im Projektmappen-Explorer einen Verweis auf die Assembly "WindowsFormsIntegration" hinzu, die WindowsFormsIntegration.dllheißt.
Fügen Sie im Projektmappen-Explorer Verweise auf die Assemblys "System.Drawing" und "System.Windows.Forms" hinzu.
Definieren des Anwendungslayouts
Die WPF-basierte Anwendung verwendet das WindowsFormsHost Element zum Hosten eines Windows Forms-Steuerelements.
So definieren Sie das Anwendungslayout
Öffnen Sie Window1.xaml im WPF-Designer.
Ersetzen Sie den vorhandenen Code durch den folgenden 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>Öffnen Sie Window1.xaml.cs im Code-Editor.
Importieren Sie oben in der Datei die folgenden Namespaces.
using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Windows.Forms.Integration;Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Windows.Forms.Integration
Definierung einer neuen Eigenschaftszuordnung
Das WindowsFormsHost Element stellt mehrere Standardeigenschaftenzuordnungen bereit. Sie fügen eine neue Eigenschaftszuordnung hinzu, indem Sie die Add Methode für das WindowsFormsHost Element PropertyMapaufrufen.
So definieren Sie eine neue Eigenschaftszuordnung
Kopieren Sie den folgenden Code in die Definition der
Window1Klasse.// 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 custom mapping ' for the Clip property. Private Sub AddClipMapping() wfHost.PropertyMap.Add( _ "Clip", _ New PropertyTranslator(AddressOf OnClipChange)) End Sub ' The OnClipChange method assigns an elliptical clipping ' region to the hosted control's Region property. Private Sub OnClipChange( _ ByVal h As Object, _ ByVal propertyName As String, _ ByVal value As Object) Dim host As WindowsFormsHost = h Dim cb As System.Windows.Forms.CheckBox = host.Child If cb IsNot Nothing Then cb.Region = Me.CreateClipRegion() End If End Sub ' 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 Sub Window1_SizeChanged( _ ByVal sender As Object, _ ByVal e As SizeChangedEventArgs) Me.OnClipChange(wfHost, "Clip", Nothing) End Sub ' The CreateClipRegion method creates a Region from an ' elliptical GraphicsPath. Private Function CreateClipRegion() As [Region] Dim path As New GraphicsPath() path.StartFigure() path.AddEllipse(New System.Drawing.Rectangle( _ 0, _ 0, _ wfHost.ActualWidth, _ wfHost.ActualHeight)) path.CloseFigure() Return New [Region](path) End FunctionDie
AddClipMappingMethode fügt eine neue Zuordnung für die Clip Eigenschaft hinzu.Die
OnClipChangeMethode übersetzt die Clip Eigenschaft zur Windows Forms-EigenschaftRegion.Die
Window1_SizeChangedMethode behandelt das Ereignis des Fensters SizeChanged und passt den Clippingbereich auf die Größe des Anwendungsfensters an.
Entfernen einer Standardeigenschaftszuordnung
Entfernen Sie eine Standardeigenschaftszuordnung, indem Sie die Remove Methode für das WindowsFormsHost Element PropertyMapaufrufen.
So entfernen Sie eine Standardeigenschaftszuordnung
Kopieren Sie den folgenden Code in die Definition der
Window1Klasse.// 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. Private Sub RemoveCursorMapping() wfHost.PropertyMap.Remove("Cursor") End SubDie
RemoveCursorMappingMethode löscht die Standardzuordnung für die Cursor Eigenschaft.
Ersetzen einer Standardeigenschaftszuordnung
Ersetzen Sie eine Standardeigenschaftszuordnung, indem Sie die Standardzuordnung entfernen und die Add Methode für das WindowsFormsHost Element PropertyMapaufrufen.
Eine Standardeigenschaftszuordnung ersetzen
Kopieren Sie den folgenden Code in die Definition der
Window1Klasse.// 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. Private Sub ReplaceFlowDirectionMapping() wfHost.PropertyMap.Remove("FlowDirection") wfHost.PropertyMap.Add( _ "FlowDirection", _ New PropertyTranslator(AddressOf OnFlowDirectionChange)) End Sub ' 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 Sub OnFlowDirectionChange( _ ByVal h As Object, _ ByVal propertyName As String, _ ByVal value As Object) Dim host As WindowsFormsHost = h Dim fd As System.Windows.FlowDirection = _ CType(value, System.Windows.FlowDirection) Dim cb As System.Windows.Forms.CheckBox = host.Child cb.RightToLeft = IIf(fd = System.Windows.FlowDirection.RightToLeft, _ RightToLeft.Yes, _ RightToLeft.No) End Sub ' 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 Sub cb_CheckedChanged( _ ByVal sender As Object, _ ByVal e As EventArgs) Dim cb As System.Windows.Forms.CheckBox = sender wfHost.FlowDirection = IIf(cb.CheckState = CheckState.Checked, _ System.Windows.FlowDirection.RightToLeft, _ System.Windows.FlowDirection.LeftToRight) End SubDie
ReplaceFlowDirectionMappingMethode ersetzt die Standardzuordnung für die FlowDirection Eigenschaft.Die
OnFlowDirectionChangeMethode übersetzt die FlowDirection Eigenschaft zur Windows Forms-EigenschaftRightToLeft.Die
cb_CheckedChangedMethode behandelt das CheckedChanged Ereignis für das CheckBox Steuerelement. Sie weist die FlowDirection Eigenschaft abhängig vom Wert der Eigenschaft CheckState zu.
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
Window1Klasse.// 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 property ' translator if a mapping already exists. Private Sub ExtendBackgroundMapping() If wfHost.PropertyMap("Background") IsNot Nothing Then wfHost.PropertyMap("Background") = PropertyTranslator.Combine( _ wfHost.PropertyMap("Background"), _ PropertyTranslator.CreateDelegate( _ GetType(PropertyTranslator), _ Me, _ "OnBackgroundChange")) End If End Sub ' The OnBackgroundChange method assigns a specific image ' to the hosted control's BackgroundImage property. Private Sub OnBackgroundChange(ByVal h As Object, ByVal propertyName As String, ByVal value As Object) Dim host As WindowsFormsHost = h Dim cb As System.Windows.Forms.CheckBox = host.Child Dim b As ImageBrush = value If Not (b Is Nothing) Then cb.BackgroundImage = New System.Drawing.Bitmap("C:\WINDOWS\Santa Fe Stucco.bmp") End If End SubDie
ExtendBackgroundMappingMethode fügt der vorhandenen Background Eigenschaftszuordnung einen benutzerdefinierten Eigenschaftenübersetzer hinzu.Die
OnBackgroundChangeMethode weist der Eigenschaft des gehosteten Steuerelements BackgroundImage ein bestimmtes Bild zu. DieOnBackgroundChangeMethode wird aufgerufen, nachdem die Standardeigenschaftszuordnung angewendet wurde.
Initialisieren Ihrer Eigenschaften-Zuordnungen
Richten Sie die Eigenschaftszuordnungen ein, indem Sie die zuvor beschriebenen Methoden im Loaded Ereignishandler aufrufen.
So initialisieren Sie Ihre Eigenschaftenzuordnungen
Kopieren Sie den folgenden Code in die Definition der
Window1Klasse.// 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. ' 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 Sub WindowLoaded( _ ByVal sender As Object, _ ByVal e As RoutedEventArgs) System.Windows.Forms.Application.EnableVisualStyles() ' Create a Windows Forms checkbox control and assign ' it as the WindowsFormsHost element's child. Dim cb As New System.Windows.Forms.CheckBox() cb.Text = "Windows Forms checkbox" cb.Dock = DockStyle.Fill cb.TextAlign = ContentAlignment.MiddleCenter AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged wfHost.Child = cb ' Replace the default mapping for the FlowDirection property. Me.ReplaceFlowDirectionMapping() ' Remove the mapping for the Cursor property. Me.RemoveCursorMapping() ' Add the mapping for the Clip property. Me.AddClipMapping() ' Add another mapping for the Background property. Me.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() End SubDie
WindowLoadedMethode behandelt das Loaded Ereignis und führt die folgende Initialisierung aus.Erstellt ein Windows Forms-SteuerelementCheckBox .
Ruft die Methoden auf, die Sie weiter oben in der exemplarischen Vorgehensweise definiert haben, um die Eigenschaftenzuordnungen einzurichten.
Weist den zugeordneten Eigenschaften Anfangswerte zu.
Drücken Sie F5-, um die Anwendung zu erstellen und auszuführen. Klicken Sie auf das Kontrollkästchen, um die Auswirkung der FlowDirection Zuordnung anzuzeigen. Wenn Sie auf das Kontrollkästchen klicken, kehrt das Layout die Ausrichtung von links rechts um.
Siehe auch
.NET Desktop feedback