DependencyObject 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表參與依賴屬性系統的物件。
public ref class DependencyObject : System::Windows::Threading::DispatcherObject
public class DependencyObject : System.Windows.Threading.DispatcherObject
[System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))]
public class DependencyObject : System.Windows.Threading.DispatcherObject
type DependencyObject = class
inherit DispatcherObject
[<System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))>]
type DependencyObject = class
inherit DispatcherObject
Public Class DependencyObject
Inherits DispatcherObject
- 繼承
- 衍生
- 屬性
範例
以下範例由 DependencyObject 創建一個新的抽象類別衍生而來。 該類別接著註冊一個附加屬性,並包含該附加屬性的支援成員。
public abstract class AquariumObject3 : DependencyObject
{
public enum Bouyancy
{
Floats,
Sinks,
Drifts
}
public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
"Bouyancy",
typeof(Bouyancy),
typeof(AquariumObject3),
new FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange),
new ValidateValueCallback(ValidateBouyancy)
);
public static void SetBouyancy(UIElement element, Bouyancy value)
{
element.SetValue(BouyancyProperty, value);
}
public static Bouyancy GetBouyancy(UIElement element)
{
return (Bouyancy)element.GetValue(BouyancyProperty);
}
private static bool ValidateBouyancy(object value)
{
Bouyancy bTest = (Bouyancy) value;
return (bTest == Bouyancy.Floats || bTest == Bouyancy.Drifts || bTest==Bouyancy.Sinks);
}
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
"IsDirty",
typeof(Boolean),
typeof(AquariumObject3)
);
}
Public MustInherit Class AquariumObject3
Inherits DependencyObject
Public Enum Bouyancy
Floats
Sinks
Drifts
End Enum
Public Shared ReadOnly BouyancyProperty As DependencyProperty = DependencyProperty.RegisterAttached("Bouyancy", GetType(Bouyancy), GetType(AquariumObject3), New FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange), New ValidateValueCallback(AddressOf ValidateBouyancy))
Public Shared Sub SetBouyancy(ByVal element As UIElement, ByVal value As Bouyancy)
element.SetValue(BouyancyProperty, value)
End Sub
Public Shared Function GetBouyancy(ByVal element As UIElement) As Bouyancy
Return CType(element.GetValue(BouyancyProperty), Bouyancy)
End Function
Private Shared Function ValidateBouyancy(ByVal value As Object) As Boolean
Dim bTest As Bouyancy = CType(value, Bouyancy)
Return (bTest = Bouyancy.Floats OrElse bTest = Bouyancy.Drifts OrElse bTest = Bouyancy.Sinks)
End Function
Public Shared ReadOnly IsDirtyProperty As DependencyProperty = DependencyProperty.Register("IsDirty", GetType(Boolean), GetType(AquariumObject3))
End Class
備註
DependencyObject 類別使其衍生類別Windows Presentation Foundation(WPF)屬性系統服務成為可能。
屬性系統的主要功能是計算屬性的值,並提供系統通知變更的值。 另一個參與屬性系統的關鍵類別是 DependencyProperty。 DependencyProperty 使得依賴屬性能註冊到屬性系統,並提供每個依賴屬性的識別與資訊,而基底類別則 DependencyObject 允許物件使用依賴屬性。
DependencyObject 服務與特性包括以下幾項:
依賴性物業託管支援。 你透過呼叫 Register 該方法,並將方法的回傳值存為類別中的公開靜態欄位來註冊相依屬性。
附帶的物業託管支援。 你透過呼叫 RegisterAttached 該方法註冊附加屬性,並將方法的回傳值存為類別中的公開靜態唯讀欄位。 (還有額外的成員要求;請注意,這是針對附加屬性的 WPF 專屬實作。詳情請參見附加屬性概覽.)你所附的屬性可以設定在任何源自 DependencyObject 的類別上。
取得、設定並清除存在於 DependencyObject的依賴性質值的效用方法。
元資料、強制值支援、屬性變更通知,以及覆寫相依屬性或附加屬性的回調。 此外,該 DependencyObject 類別也促進了依賴屬性的每個擁有者屬性元資料。
一個 ContentElement通用的基底類別,用於由 、 Freezable、 或 Visual衍生的類別。 (UIElement另一個基底元素類別,具有包含的 Visual類別階層。)
建構函式
| 名稱 | Description |
|---|---|
| DependencyObject() |
初始化 DependencyObject 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| DependencyObjectType |
會取得 DependencyObjectType 包裹此實例 CLR 類型的 。 |
| Dispatcher |
了解 Dispatcher 這與此 DispatcherObject 有關。 (繼承來源 DispatcherObject) |
| IsSealed |
會獲得一個值,表示該實例目前是否封存(唯讀)。 |