Share via


Windows Form 和 WPF 屬性對應

更新:2007 年 11 月

Windows Form 與 WPF 技術有兩種類似但不同的屬性模型。「屬性對應」(Property Mapping) 支援兩個架構之間的互通操作,並提供下列功能:

  • 輕鬆將主機環境中的相關屬性變更對應至所裝載的控制項或項目。

  • 提供用以對應最常用屬性的預設處理。

  • 可以輕鬆移除、覆寫或擴充預設屬性。

  • 確保會自動偵測主應用程式 (Host) 上的屬性值變更並加以轉譯成所裝載的控制項或項目。

注意事項:

屬性變更事件不會在裝載控制項或項目階層中向上傳播。如果屬性的本機值並未因為直接設定、樣式、繼承 (Inheritance)、資料繫結或其他會變更屬性值的機制而變更,則不會執行屬性轉譯。

WindowsFormsHost 項目使用 PropertyMap 屬性,以及對 ElementHost 控制項使用 PropertyMap 屬性,即可存取屬性對應。

WindowsFormsHost 項目的屬性對應

WindowsFormsHost 項目會根據下列轉譯表,將預設 WPF 屬性轉譯為其 Windows Form 對等用法。

Windows Presentation Foundation 裝載

Windows Form

互通行為

Background

(System.Windows.Media.Brush)

BackColor

(System.Drawing.Color)

WindowsFormsHost 項目會設定裝載之控制項的 BackColor 屬性和裝載之控制項的 BackgroundImage 屬性。系統會使用下列規則來執行對應:

  • 如果 Background 為純色,則會將其轉換並用於設定裝載之控制項的 BackColor 屬性。由於裝載的控制項可以繼承 BackColor 屬性的值,所以不會對裝載的控制項設定 BackColor 屬性。

注意事項:

裝載的控制項不支援透明度。任何指派給 BackColor 的色彩都必須完全不透明,其 Alpha 值為 0xFF。

注意事項:

您可以覆寫這個行為,也可以移除 Background 屬性對應。

Cursor

Cursor

如果尚未重新指派預設對應,則 WindowsFormsHost 控制項會周遊其祖系階層,直到它找到已設定其 Cursor 屬性的祖系為止。然後會將這個值轉譯成最接近的對應 Windows Form 游標。

如果尚未重新指派 ForceCursor 屬性的預設對應,則周遊作業會停在 ForceCursor 設定為 true 的第一個祖系上。

FlowDirection

(System.Windows.FlowDirection)

RightToLeft

(System.Windows.Forms.RightToLeft)

LeftToRight 對應到 No

RightToLeft 對應到 Yes

Inherit 沒有對應。

FlowDirection.RightToLeft 對應到 RightToLeft.Yes

FontStyle

所裝載控制項之 System.Drawing.FontStyle

這組 WPF 屬性會轉譯為對應的 Font。當其中一個屬性變更時,便會建立新的 Font。如果是 NormalItalic 已停用。如果是 ItalicObliqueItalic 已啟用。

FontWeight

所裝載控制項之 System.Drawing.FontStyle

這組 WPF 屬性會轉譯為對應的 Font。當其中一個屬性變更時,便會建立新的 Font。如果是 BlackBoldDemiBoldExtraBoldHeavyMediumSemiBoldUltraBoldBold 已啟用。 如果是 ExtraLightLightNormalRegularThinUltraLightBold 已停用。

FontFamily

FontSize

FontStretch

FontStyle

FontWeight

Font

(System.Drawing.Font)

這組 WPF 屬性會轉譯為對應的 Font。當其中一個屬性變更時,便會建立新的 Font。裝載的 Windows Form 控制項會根據字型大小而調整大小。

WPF 中的字型大小會以 1/96 英吋表示,而在 Windows Form 中則以 1/72 英吋表示。對應的轉換如下:

Windows Form 字型大小 = WPF 字型大小 * 72.0 / 96.0。

Foreground

(System.Windows.Media.Brush)

ForeColor

(System.Drawing.Color)

系統是藉由使用下列規則來執行 Foreground 屬性對應:

IsEnabled

Enabled

設定 IsEnabled 時,WindowsFormsHost 項目會對裝載的控制項設定 Enabled 屬性。

Padding

Padding

所裝載 Windows Form 控制項的四個 Padding 屬性值都會設定為相同的 Thickness 值。

Visibility

Visible

  • Visible 對應到 Visible = true。裝載的 Windows Form 控制項為可見的。不建議將所裝載控制項的 Visible 屬性明確設定為 false。

  • Collapsed 對應到 Visible = true 或 false。裝載的 Windows Form 控制項不會進行繪製,且其區域會摺疊。

  • Hidden:裝載的 Windows Form 控制項會佔用配置空間,但卻看不見。在此情況下,Visible 屬性會設定為 true。不建議將所裝載控制項的 Visible 屬性明確設定為 false。

WindowsFormsHost 項目完全支援容器項目上的附加屬性。

如需詳細資訊,請參閱逐步解說:使用 WindowsFormsHost 項目對應屬性

更新父屬性

變更大部分的父屬性會導致傳送告知給裝載的子控制項。下列清單說明其值變更時不會產生告知的屬性。

例如,若您變更 WindowsFormsHost 項目的 Background 屬性值,則所裝載控制項的 BackColor 屬性不會改變。

ElementHost 控制項的屬性對應

下列屬性提供內建變更告知。對應這些屬性時,請勿呼叫 OnPropertyChanged 方法:

  • AutoSize

  • BackColor

  • BackgroundImage

  • BackgroundImageLayout

  • BindingContext

  • CausesValidation

  • ContextMenu

  • ContextMenuStrip

  • Cursor

  • Dock

  • Enabled

  • Font

  • ForeColor

  • Location

  • Margin

  • Padding

  • Parent

  • Region

  • RightToLeft

  • Size

  • TabIndex

  • TabStop

  • Text

  • Visible

ElementHost 控制項會根據下列轉譯表,將預設的 Windows Form 屬性轉譯成其 WPF 對等用法。

如需詳細資訊,請參閱逐步解說:使用 ElementHost 控制項對應屬性

Windows Form 裝載

Windows Presentation Foundation

互通行為

BackColor

(System.Drawing.Color)

Background

所裝載項目的 (System.Windows.Media.Brush)

設定這個屬性會強制使用 ImageBrush 重新繪製。如果 BackColorTransparent 屬性設定為 false (預設值),這個 ImageBrush 就會以 ElementHost 控制項的外觀為基礎,包含其 BackColorBackgroundImageBackgroundImageLayout 屬性,以及所有附加的繪製處理常式。

如果 BackColorTransparent 屬性設定為 true,則 ImageBrush 會以 ElementHost 控制項的父代 (Parent) 外觀為基礎,包含父代的 BackColorBackgroundImageBackgroundImageLayout 屬性,以及所有附加的繪製處理常式。

BackgroundImage

(System.Drawing.Image)

Background

所裝載項目的 (System.Windows.Media.Brush)

設定這個屬性會造成針對 BackColor 對應所描述的相同行為。

BackgroundImageLayout

Background

所裝載項目的 (System.Windows.Media.Brush)

設定這個屬性會造成針對 BackColor 對應所描述的相同行為。

Cursor

(System.Windows.Forms.Cursor)

Cursor

(System.Windows.Input.Cursor)

Windows Form 標準游標會轉譯為對應的 WPF 標準游標。如果 Windows Form 不是標準游標,則會指派預設值。

Enabled

IsEnabled

設定 Enabled 時,ElementHost 控制項會對裝載的項目設定 IsEnabled 屬性。

Font

(System.Drawing.Font)

FontFamily

FontSize

FontStretch

FontStyle

FontWeight

Font 值會轉譯成對應的 WPF 字型屬性集。

Bold

所裝載項目的 FontWeight

如果 Bold 為 true,FontWeight 會設定為 Bold

如果 Bold 為 false,FontWeight 會設定為 Normal

Italic

所裝載項目的 FontStyle

如果 Italic 為 true,FontStyle 會設定為 Italic

如果 Italic 為 false,FontStyle 會設定為 Normal

Strikeout

所裝載項目的 TextDecorations

只有在裝載 TextBlock 控制項時才適用。

Underline

所裝載項目的 TextDecorations

只有在裝載 TextBlock 控制項時才適用。

RightToLeft

(System.Windows.Forms.RightToLeft)

FlowDirection

(FlowDirection)

No 對應到 LeftToRight

Yes 對應到 RightToLeft

Visible

Visibility

ElementHost 控制項會使用下列規則,對裝載的控制項設定 Visibility 屬性:

請參閱

工作

逐步解說:使用 WindowsFormsHost 項目對應屬性

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

概念

WPF 和 Win32 互通性概觀

Windows Presentation Foundation 和 Windows Form 互通性中支援的案例

參考

ElementHost

WindowsFormsHost