Windows Form 和 WPF 屬性對應Windows Forms and WPF Property Mapping

Windows FormsWindows FormsWPFWPF技術有兩個相似但不同的屬性模型。The Windows FormsWindows Forms and WPFWPF 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.

注意

屬性變更事件不會在裝載控制項或元素階層中向上傳播。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.

PropertyMap ElementHost使用專案PropertyMap WindowsFormsHost上的屬性和控制項上的屬性來存取屬性對應。Use the PropertyMap property on the WindowsFormsHost element and the PropertyMap property on ElementHost control to access property mapping.

具有 WindowsFormsHost 元素的屬性對應Property Mapping with the WindowsFormsHost Element

元素會使用下列WPFWPF轉譯表, Windows FormsWindows Forms將預設屬性轉換為其對等專案。 WindowsFormsHostThe WindowsFormsHost element translates default WPFWPF properties to their Windows FormsWindows Forms equivalents using the following translation table.

Windows Presentation Foundation 裝載Windows Presentation Foundation hosting Windows FormsWindows Forms 交互操作行為Interoperation behavior
Background

(System.Windows.Media.Brush)(System.Windows.Media.Brush)
BackColor

(System.Drawing.Color)(System.Drawing.Color)
元素會設定裝載控制項的BackgroundImage 屬性,以及裝載控制項的屬性。BackColor WindowsFormsHostThe 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:

-如果Background是純色, 則會轉換並用來設定裝載控制項的BackColor屬性。- If Background is a solid color, it is converted and used to set the BackColor property of the hosted control. 未在裝載的控制項上設定BackColor 屬性,因為裝載的控制項可以繼承屬性的值。BackColorThe BackColor property is not set on the hosted control, because the hosted control can inherit the value of the BackColor property. 注意: 裝載的控制項不支援透明度。Note: The hosted control does not support transparency. 指派給BackColor的任何色彩都必須是完全不透明的, 且 Alpha 值為0xff。Any color assigned to BackColor must be fully opaque, with an alpha value of 0xFF.

-如果Background不是純色WindowsFormsHost , 控制項會從Background屬性建立點陣圖。- If Background is not a solid color, the WindowsFormsHost control creates a bitmap from the Background property. 控制項會將這個點陣圖指派給裝載BackgroundImage控制項的屬性。 WindowsFormsHostThe WindowsFormsHost control assigns this bitmap to the BackgroundImage property of the hosted control. 這會提供類似于透明度的效果。This provides an effect which is similar to transparency. 注意: 您可以覆寫此行為, 也可以移除Background屬性對應。Note: You can override this behavior or you can remove the Background property mapping.
Cursor Cursor 如果尚未重新指派預設的對應, 控制權WindowsFormsHost就會遍歷其上階階層, 直到找到其Cursor屬性設定的上階為止。If the default mapping has not been reassigned, WindowsFormsHost control traverses its ancestor hierarchy until it finds an ancestor with its Cursor property set. 這個值會轉譯為最接近的Windows FormsWindows Forms對應資料指標。This value is translated to the closest corresponding Windows FormsWindows Forms cursor.

如果尚未重新指派ForceCursor屬性的預設對應, 則會在ForceCursor設定為true的第一個上階上停止遍歷。If the default mapping for the ForceCursor property has not been reassigned, the traversal stops on the first ancestor with ForceCursor set to true.
FlowDirection

(System.Windows.FlowDirection)(System.Windows.FlowDirection)
RightToLeft

(System.Windows.Forms.RightToLeft)(System.Windows.Forms.RightToLeft)
LeftToRight對應至NoLeftToRight maps to No.

RightToLeft對應至YesRightToLeft maps to Yes.

Inherit未對應。Inherit is not mapped.

FlowDirection.RightToLeft對應至RightToLeft.YesFlowDirection.RightToLeft maps to RightToLeft.Yes.
FontStyle Style在主控控制項的System.Drawing.FontStyle on the hosted control's System.Drawing.Font WPFWPF屬性集會轉譯成對應Font的。The set of WPFWPF properties is translated into a corresponding Font. 當其中一個屬性變更時, 就會Font建立新的。When one of these properties changes, a new Font is created. 針對Normal :Italic已停用。For Normal: Italic is disabled. 針對ItalicOblique :Italic已啟用。For Italic or Oblique: Italic is enabled.
FontWeight Style在主控控制項的System.Drawing.FontStyle on the hosted control's System.Drawing.Font WPFWPF屬性集會轉譯成對應Font的。The set of WPFWPF properties is translated into a corresponding Font. 當其中一個屬性變更時, 就會Font建立新的。When one of these properties changes, a new Font is created. 對於BlackBoldDemiBold、、 、Medium、或:UltraBold已啟用BoldExtraBold Heavy SemiBoldFor Black, Bold, DemiBold, ExtraBold, Heavy, Medium, SemiBold, or UltraBold: Bold is enabled. 對於ExtraLightLight、 、、UltraLight或:已Bold停用。 Regular Normal ThinFor ExtraLight, Light, Normal, Regular, Thin, or UltraLight: Bold is disabled.
FontFamily

FontSize

FontStretch

FontStyle

FontWeight
Font

(System.Drawing.Font)(System.Drawing.Font)
WPFWPF屬性集會轉譯成對應Font的。The set of WPFWPF properties is translated into a corresponding Font. 當其中一個屬性變更時, 就會Font建立新的。When one of these properties changes, a new Font is created. 裝載的Windows FormsWindows Forms控制項會根據字型大小調整大小。The hosted Windows FormsWindows Forms control resizes based on the font size.

WPFWPF的字型大小會表示為一英寸的 90-6, 而在中Windows FormsWindows Forms則會以一英寸的一到1個。Font size in WPFWPF is expressed as one ninety-sixth of an inch, and in Windows FormsWindows Forms as one seventy-second of an inch. 對應的轉換如下:The corresponding conversion is:

Windows FormsWindows Forms字型大小 = WPFWPF字型大小 * 72.0/96.0。font size = WPFWPF font size * 72.0 / 96.0.
Foreground

(System.Windows.Media.Brush)(System.Windows.Media.Brush)
ForeColor

(System.Drawing.Color)(System.Drawing.Color)
Foreground屬性對應是使用下列規則來執行:The Foreground property mapping is performed by using the following rules:

-如果Foreground Color是, 請使用做ForeColor為。 SolidColorBrush- If Foreground is a SolidColorBrush, use Color for ForeColor.
-如果Foreground GradientStop是, 請使用具有最低位移值ForeColor的色彩。 GradientBrush- If Foreground is a GradientBrush, use the color of the GradientStop with the lowest offset value for ForeColor.
-若為任何Brush其他類型, ForeColor請保持不變。- For any other Brush type, leave ForeColor unchanged. 這表示會使用預設值。This means the default is used.
IsEnabled Enabled IsEnabled設定時, WindowsFormsHost元素會在Enabled裝載的控制項上設定屬性。When IsEnabled is set, WindowsFormsHost element sets the Enabled property on the hosted control.
Padding Padding 裝載Padding Windows FormsWindows Forms控制項上屬性的四個值都會設定為相同Thickness的值。All four values of the Padding property on the hosted Windows FormsWindows Forms control are set to the same Thickness value.

-大於MaxValue的值會設定為MaxValue- Values greater than MaxValue are set to MaxValue.
-小於MinValue的值會設定為MinValue- Values less than MinValue are set to MinValue.
Visibility Visible - Visible對應至Visible。 = true- Visible maps to Visible = true. 裝載的Windows FormsWindows Forms控制項是可見的。The hosted Windows FormsWindows Forms control is visible. 不建議將Visible裝載控制項上的屬性明確false設定為。Explicitly setting the Visible property on the hosted control to false is not recommended.
- Collapsed對應至Visible = 或。true false- Collapsed maps to Visible = true or false. 裝載的Windows FormsWindows Forms控制項不會繪製, 而且其區域會折迭。The hosted Windows FormsWindows Forms control is not drawn, and its area is collapsed.
- Hidden:裝載的Windows FormsWindows Forms控制項會佔用配置中的空間, 但看不到。- Hidden : The hosted Windows FormsWindows Forms control occupies space in the layout, but is not visible. 在此情況下, Visible屬性會設定為trueIn this case, the Visible property is set to true. 不建議將Visible裝載控制項上的屬性明確false設定為。Explicitly setting the Visible property on the hosted control to false is not recommended.

元素會完全支援容器元素上的WindowsFormsHost附加屬性。Attached properties on container elements are fully supported by the WindowsFormsHost element.

如需詳細資訊,請參閱逐步解說:使用 WindowsFormsHost 元素對應屬性。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 WindowsFormsHost元素的屬性值, BackColor則裝載控制項的屬性不會變更。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.

使用 ElementHost 控制項的屬性對應Property Mapping with the ElementHost Control

下列屬性會提供內建的變更通知。The following properties provide built-in change notification. 當您對應這些OnPropertyChanged屬性時, 請勿呼叫方法:Do not call the OnPropertyChanged method when you are mapping these properties:

  • AutoSizeAutoSize

  • BackColorBackColor

  • BackgroundImageBackgroundImage

  • BackgroundImageLayoutBackgroundImageLayout

  • BindingContextBindingContext

  • CausesValidationCausesValidation

  • ContextMenuContextMenu

  • ContextMenuStripContextMenuStrip

  • 資料指標Cursor

  • 停駐Dock

  • 已啟用Enabled

  • 字型Font

  • ForeColorForeColor

  • 位置Location

  • 邊界Margin

  • 與邊框距離Padding

  • 父代Parent

  • 區域Region

  • RightToLeftRightToLeft

  • 大小Size

  • TabIndexTabIndex

  • TabStopTabStop

  • 文字Text

  • VisibleVisible

控制項會使用下列翻譯Windows FormsWindows Forms表, 將WPFWPF預設屬性轉換為其對等專案。 ElementHostThe ElementHost control translates default Windows FormsWindows Forms properties to their WPFWPF equivalents by using the following translation table.

如需詳細資訊,請參閱逐步解說:使用 ElementHost 控制項對應屬性。For more information, see Walkthrough: Mapping Properties Using the ElementHost Control.

Windows Forms 裝載Windows Forms hosting Windows Presentation FoundationWindows Presentation Foundation 交互操作行為Interoperation behavior
BackColor

(System.Drawing.Color)(System.Drawing.Color)
Background

(System.Windows.Media.Brush) 在託管元素上(System.Windows.Media.Brush) on the hosted element
設定此屬性會強制重新繪製ImageBrushSetting this property forces a repaint with an ImageBrush. ElementHost ImageBrush BackgroundImage BackColor BackgroundImageLayout如果屬性設為false (預設值), 則這是根據控制項的外觀, 包括其、、屬性和任何附加的繪製BackColorTransparent處理常式.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.

ElementHost true BackColor BackgroundImageLayout BackgroundImage如果屬性設定為, ImageBrush則會以控制項父系的外觀為基礎, 包括父系的、、屬性和任何附加的繪製BackColorTransparent處理常式.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

(System.Drawing.Image)(System.Drawing.Image)
Background

(System.Windows.Media.Brush) 在託管元素上(System.Windows.Media.Brush) on the hosted element
設定此屬性會導致BackColor對應所述的相同行為。Setting this property causes the same behavior described for the BackColor mapping.
BackgroundImageLayout Background

(System.Windows.Media.Brush) 在託管元素上(System.Windows.Media.Brush) on the hosted element
設定此屬性會導致BackColor對應所述的相同行為。Setting this property causes the same behavior described for the BackColor mapping.
Cursor

(System.Windows.Forms.Cursor)(System.Windows.Forms.Cursor)
Cursor

(System.Windows.Input.Cursor)(System.Windows.Input.Cursor)
標準資料指標會轉譯成對應WPFWPF的標準資料指標。 Windows FormsWindows FormsThe Windows FormsWindows Forms standard cursor is translated to the corresponding WPFWPF standard cursor. Windows FormsWindows Forms如果不是標準資料指標, 則會指派預設值。If the Windows FormsWindows Forms is not a standard cursor, the default is assigned.
Enabled IsEnabled Enabled設定時ElementHost , 控制項會在裝載的IsEnabled元素上設定屬性。When Enabled is set, the ElementHost control sets the IsEnabled property on the hosted element.
Font

(System.Drawing.Font)(System.Drawing.Font)
FontFamily

FontSize

FontStretch

FontStyle

FontWeight
Font值會轉譯成一組對應的WPFWPF字型屬性。The Font value is translated into a corresponding set of WPFWPF font properties.
Bold FontWeight在託管元素上FontWeight on hosted element 如果 Boldtrue,則會將 FontWeight 設為 BoldIf Bold is true, FontWeight is set to Bold.

如果 Boldfalse,則會將 FontWeight 設為 NormalIf Bold is false, FontWeight is set to Normal.
Italic FontStyle在託管元素上FontStyle on hosted element 如果 Italictrue,則會將 FontStyle 設為 ItalicIf Italic is true, FontStyle is set to Italic.

如果 Italicfalse,則會將 FontStyle 設為 NormalIf Italic is false, FontStyle is set to Normal.
Strikeout TextDecorations在託管元素上TextDecorations on hosted element 僅適用于裝載TextBlock控制項時。Applies only when hosting a TextBlock control.
Underline TextDecorations在託管元素上TextDecorations on hosted element 僅適用于裝載TextBlock控制項時。Applies only when hosting a TextBlock control.
RightToLeft

(System.Windows.Forms.RightToLeft)(System.Windows.Forms.RightToLeft)
FlowDirection

(FlowDirection)(FlowDirection)
No對應至LeftToRightNo maps to LeftToRight.

Yes對應至RightToLeftYes maps to RightToLeft.
Visible Visibility 控制項會使用下列規則Visibility , 在裝載的元素上設定屬性: ElementHostThe ElementHost control sets the Visibility property on the hosted element by using the following rules:

- Visible = true對應至Visible- Visible = true maps to Visible.
- Visible = false對應至Hidden- Visible = false maps to Hidden.

另請參閱See also