DependencyProperty.RegisterAttached 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
註冊附加的相依屬性,並包含指定的屬性名稱、屬性類型、所有者類型及屬性元資料。
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
要註冊的相依屬性名稱。
- defaultMetadata
- PropertyMetadata
屬性元資料實例。 這可以包含 PropertyChangedCallback 實作參考。
傳回
一個相依屬性識別碼,應該用來設定你類別中公開靜態唯讀欄位的值。 該識別碼之後會用來參考附加屬性,例如程式設定其值或附加 綁定。
範例
此範例定義了一個源自 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);
}
}