Windows Forms and WPF Property Mapping
The Windows Forms and WPF technologies have two similar but different property models. Property mapping supports interoperation between the two architectures and provides the following capabilities:
Makes it easy to map relevant property changes in the host environment to the hosted control or element.
Provides default handling for mapping the most commonly used properties.
Allows easy removal, overriding, or extending of default properties.
Ensures that property value changes on the host are automatically detected and translated to the hosted control or element.
Note: |
---|
Property-change events are not propagated up the hosting control or element hierarchy. Property translation is not performed if the local value of a property does not change because of direct setting, styles, inheritance, data binding, or other mechanisms that change the value of the property. |
Use the PropertyMap property on the WindowsFormsHost element and the PropertyMap property on ElementHost control to access property mapping.
Property Mapping with the WindowsFormsHost Element
The WindowsFormsHost element translates default WPF properties to their Windows Forms equivalents using the following translation table.
Windows Presentation Foundation hosting | Windows Forms | Interoperation behavior | ||||
---|---|---|---|---|---|---|
The WindowsFormsHost element sets the BackColor property of the hosted control and the BackgroundImage property of the hosted control. Mapping is performed by using the following rules:
|
||||||
If the default mapping has not been reassigned, WindowsFormsHost control traverses its ancestor hierarchy until it finds an ancestor with its Cursor property set. This value is translated to the closest corresponding Windows Forms cursor. If the default mapping for the ForceCursor property has not been reassigned, the traversal stops on the first ancestor with ForceCursor set to true. |
||||||
LeftToRight maps to No. RightToLeft maps to Yes. Inherit is not mapped. System.Windows.FlowDirection.RightToLeft maps to System.Windows.Forms.RightToLeft.Yes. |
||||||
Style on the hosted control's System.Drawing.Font |
The set of WPF properties is translated into a corresponding Font. When one of these properties changes, a new Font is created. For Normal: Italic is disabled. For Italic or Oblique: Italic is enabled. |
|||||
Style on the hosted control's System.Drawing.Font |
The set of WPF properties is translated into a corresponding Font. When one of these properties changes, a new Font is created. For Black, Bold, DemiBold, ExtraBold, Heavy, Medium, SemiBold, or UltraBold: Bold is enabled. For ExtraLight, Light, Normal, Regular, Thin, or UltraLight: Bold is disabled. |
|||||
FontStyle FontWeight |
(System.Drawing.Font) |
The set of WPF properties is translated into a corresponding Font. When one of these properties changes, a new Font is created. The hosted Windows Forms control resizes based on the font size. Font size in WPF is expressed as one ninety-sixth of an inch, and in Windows Forms as one seventy-second of an inch. The corresponding conversion is: Windows Forms font size = WPF font size * 72.0 / 96.0. |
||||
(System.Windows.Media.Brush) |
(System.Drawing.Color) |
The Foreground property mapping is performed by using the following rules:
|
||||
When IsEnabled is set, WindowsFormsHost element sets the Enabled property on the hosted control. |
||||||
All four values of the Padding property on the hosted Windows Forms control are set to the same Thickness value. |
||||||
|
Attached properties on container elements are fully supported by the WindowsFormsHost element.
For more information, see Walkthrough: Mapping Properties Using the WindowsFormsHost Element.
Updates to Parent Properties
Changes to most parent properties cause notifications to the hosted child control. The following list describes properties which do not cause notifications when their values change.
Background
Cursor
ForceCursor
Visibility
For example, if you change the value of the Background property of the WindowsFormsHost element, the BackColor property of the hosted control does not change.
Property Mapping with the ElementHost Control
The following properties provide built-in change notification. Do not call the OnPropertyChanged method when you are mapping these properties:
AutoSize
BackColor
BackgroundImage
BackgroundImageLayout
BindingContext
CausesValidation
ContextMenu
ContextMenuStrip
Cursor
Dock
Enabled
Font
ForeColor
Location
Margin
Padding
Parent
Region
RightToLeft
Size
TabIndex
TabStop
Text
Visible
The ElementHost control translates default Windows Forms properties to their WPF equivalents by using the following translation table.
For more information, see Walkthrough: Mapping Properties Using the ElementHost Control.
Windows Forms hosting | Windows Presentation Foundation | Interoperation behavior |
---|---|---|
BackColor (System.Drawing.Color) |
Background (System.Windows.Media.Brush) on the hosted element |
Setting this property forces a repaint with an ImageBrush. If the BackColorTransparent property is set to false (the default value), this ImageBrush is based on the appearance of the ElementHost control, including its BackColor, BackgroundImage, BackgroundImageLayout properties, and any attached paint handlers. If the BackColorTransparent property is set to true, the ImageBrush is based on the appearance of the ElementHost control's parent, including the parent's BackColor, BackgroundImage, BackgroundImageLayout properties, and any attached paint handlers. |
BackgroundImage |
Background (System.Windows.Media.Brush) on the hosted element |
Setting this property causes the same behavior described for the BackColor mapping. |
BackgroundImageLayout |
Background (System.Windows.Media.Brush) on the hosted element |
Setting this property causes the same behavior described for the BackColor mapping. |
Cursor |
Cursor |
The Windows Forms standard cursor is translated to the corresponding WPF standard cursor. If the Windows Forms is not a standard cursor, the default is assigned. |
Enabled |
IsEnabled |
When Enabled is set, the ElementHost control sets the IsEnabled property on the hosted element. |
Font (System.Drawing.Font) |
FontFamily FontSize FontStretch FontStyle FontWeight |
The Font value is translated into a corresponding set of WPF font properties. |
FontWeight on hosted element |
If Bold is true, FontWeight is set to Bold. If Bold is false, FontWeight is set to Normal. |
|
FontStyle on hosted element |
If Italic is true, FontStyle is set to Italic. If Italic is false, FontStyle is set to Normal. |
|
TextDecorations on hosted element |
Applies only when hosting a TextBlock control. |
|
TextDecorations on hosted element |
Applies only when hosting a TextBlock control. |
|
RightToLeft (System.Windows.Forms.RightToLeft) |
FlowDirection (FlowDirection) |
No maps to LeftToRight. Yes maps to RightToLeft. |
Visible |
Visibility |
The ElementHost control sets the Visibility property on the hosted element by using the following rules:
|
See Also
Tasks
Walkthrough: Mapping Properties Using the WindowsFormsHost Element
Walkthrough: Mapping Properties Using the ElementHost Control
Reference
Concepts
WPF and Win32 Interoperation Overview
Supported Scenarios in Windows Presentation Foundation and Windows Forms Interoperation