PropertyMap 類別

定義

提供一種方式,以轉譯 Windows Forms 控制項和 Windows Presentation Foundation (WPF) 項目之間的屬性值。

public ref class PropertyMap
public class PropertyMap
[System.Security.SecurityCritical]
public class PropertyMap
type PropertyMap = class
[<System.Security.SecurityCritical>]
type PropertyMap = class
Public Class PropertyMap
繼承
PropertyMap
屬性

範例

下列程式碼範例示範如何將 屬性的 Margin 對應新增至 ElementHost 控制項。

// 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

下列程式碼範例示範如何取代 控制項上 WindowsFormsHost 屬性的預設對應 FlowDirection

// 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 Sub

備註

使用 類別 PropertyMap ,在混合式應用程式中定義Windows Forms屬性與Windows Presentation Foundation (WPF) 屬性之間的轉譯。 ElementHost.PropertyMap和 類別上的 ElementHostWindowsFormsHost.PropertyMap WindowsFormsHost 屬性會定義從一項技術到另一個技術的對應。

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

建構函式

PropertyMap()

初始化 PropertyMap 類別的新執行個體。

PropertyMap(Object)

使用指定的來源物件,初始化 PropertyMap 類別的新執行個體。

屬性

DefaultTranslators

取得依預設定義的屬性對應集合。

Item[String]

取得或設定指定屬性的 PropertyTranslator 委派。

Keys

取得 ICollection 物件,其中包含 PropertyMap 集合中的屬性名稱。

SourceObject

取得含有要轉譯屬性的物件。

Values

取得 ICollection,其包含在 PropertyMap 集合中的屬性轉譯器。

方法

Add(String, PropertyTranslator)

將指定屬性的 PropertyTranslator 委派加入 PropertyMap

Apply(String)

根據來源物件目前的屬性值,針對指定屬性執行屬性翻譯。

ApplyAll()

根據來源物件的目前屬性值,針對每個對應的屬性執行屬性翻譯。

Clear()

移除所有的屬性對應。

Contains(String)

取得指出指定屬性是否對應的值。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(String)

從對應中刪除指定的屬性。

Reset(String)

還原指定屬性的預設屬性對應。

ResetAll()

還原預設的屬性對應。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

PropertyMappingError

當屬性轉譯器引發例外狀況時發生。

適用於

另請參閱