DependencyProperty.RegisterAttached 方法

定義

使用屬性的指定屬性名稱、屬性類型、擁有者類型和屬性中繼資料註冊附加的相依性屬性。

 static DependencyProperty RegisterAttached(winrt::hstring const& name, TypeName const& propertyType, TypeName const& ownerType, PropertyMetadata const& defaultMetadata);
public static DependencyProperty RegisterAttached(string name, System.Type propertyType, System.Type ownerType, PropertyMetadata defaultMetadata);
function registerAttached(name, propertyType, ownerType, defaultMetadata)
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyProperty

參數

name
String

winrt::hstring

要註冊之相依性屬性的名稱。

propertyType
TypeName Type

屬性的類型,做為 System.Type for Microsoft .NET 的類型參考 (,這是 Visual C++ 元件延伸模組的 TypeName 協助程式結構, (C++/CX) ) 。

ownerType
TypeName Type

註冊相依性屬性的擁有者類型,做為類型參考 (System.Type for Microsoft .NET,這是 Visual C++ 元件延伸模組的 TypeName 協助程式結構, (C++/CX) ) 。

defaultMetadata
PropertyMetadata

屬性中繼資料實例。 這可以包含 PropertyChangedCallback 實作參考。

傳回

相依性屬性識別碼,應該用來設定類別中公用靜態唯讀欄位的值。 然後,該識別碼會用於稍後參考附加屬性,例如以程式設計方式設定其值或附加 Binding等作業。

範例

這個範例會定義衍生自 DependencyObject的類別,並定義附加屬性以及識別碼欄位。 此類別的案例是一種服務類別,它會宣告附加屬性,讓其他 UI 元素可以在 XAML 中設定,而且服務可能會在執行時間對這些 UI 元素的附加屬性值採取動作。 如需更多範例,請參閱 自訂附加屬性

public abstract class AquariumServices : DependencyObject
{
    public enum Buoyancy {Floats,Sinks,Drifts}

    public static readonly DependencyProperty BuoyancyProperty = DependencyProperty.RegisterAttached(
      "Buoyancy",
      typeof(Buoyancy),
      typeof(AquariumServices),
      new PropertyMetadata(Buoyancy.Floats)
    );
    public static void SetBuoyancy(DependencyObject element, Buoyancy value)
    {
        element.SetValue(BuoyancyProperty, value);
    }
    public static Buoyancy GetBuoyancy(DependencyObject element)
    {
        return (Buoyancy)element.GetValue(BuoyancyProperty);
    }
}
Public Class AquariumServices
    Inherits DependencyObject
    Public Enum Buoyancy
        Floats
        Sinks
        Drifts
    End Enum

    Public Shared ReadOnly BuoyancyProperty As DependencyProperty = _
          DependencyProperty.RegisterAttached(
          "Buoyancy", _
          GetType(Buoyancy), _
          GetType(AquariumServices), _
          New PropertyMetadata(Buoyancy.Floats))


    Public Sub SetBuoyancy(element As DependencyObject, value As Buoyancy)
        element.SetValue(BuoyancyProperty, value)
    End Sub
    Public Function GetBuoyancy(element As DependencyObject) As Buoyancy
        GetBuoyancy = CType(element.GetValue(BuoyancyProperty), Buoyancy)
    End Function
End Class

適用於

另請參閱