DependencyProperty 類別

定義

代表可以透過方法設定的屬性,例如樣式、資料繫結、動畫和繼承。Represents a property that can be set through methods such as, styling, data binding, animation, and inheritance.

public ref class DependencyProperty sealed
[System.ComponentModel.TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
[System.ComponentModel.TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public sealed class DependencyProperty
type DependencyProperty = class
Public NotInheritable Class DependencyProperty
繼承
DependencyProperty
屬性

備註

DependencyProperty 支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)中的下列功能:A DependencyProperty supports the following capabilities in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF):

  • 屬性可以在樣式中設定。The property can be set in a style. 如需詳細資訊,請參閱 設定樣式和範本For more information, see Styling and Templating.

  • 屬性可以透過資料系結來設定。The property can be set through data binding. 如需資料系結相依性屬性的詳細資訊,請參閱 如何:系結兩個控制項的屬性For more information about data binding dependency properties, see How to: Bind the Properties of Two Controls.

  • 屬性可以使用動態資源參考來設定。The property can be set with a dynamic resource reference. 如需詳細資訊,請參閱 XAML 資源For more information, see XAML Resources.

  • 屬性可以從專案樹狀結構中的父元素自動繼承其值。The property can inherit its value automatically from a parent element in the element tree. 如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

  • 屬性可以進行動畫。The property can be animated. 如需詳細資訊,請參閱 動畫概觀For more information, see Animation Overview.

  • 屬性可以在屬性的先前值已變更,而且可以強制轉型屬性值時報告。The property can report when the previous value of the property has been changed and the property value can be coerced. 如需詳細資訊,請參閱相依性屬性回呼和驗證For more information, see Dependency Property Callbacks and Validation.

  • 屬性會將資訊報告給 WPFWPF,例如,變更屬性值是否應該要求配置系統 recompose 專案的視覺效果。The property reports information to WPFWPF, such as whether changing a property value should require the layout system to recompose the visuals for an element.

  • 屬性會接收 WPF Designer for Visual StudioWPF Designer for Visual Studio中的支援。The property receives support in the WPF Designer for Visual StudioWPF Designer for Visual Studio. 例如,您可以在 [屬性] 視窗中編輯屬性。For example, the property can be edited in the Properties window.

若要深入瞭解相依性屬性,請參閱相依性屬性總覽To learn more about dependency properties, see Dependency Properties Overview. 如果您想要自訂類型上的屬性,以支援上述清單中的功能,您應該建立相依性屬性。If you want properties on your custom types to support the capabilities in the preceding list, you should create a dependency property. 若要瞭解如何建立自訂相依性屬性,請參閱自訂相依性屬性。To learn how to create custom dependency properties, see Custom Dependency Properties.

附加屬性是一個屬性,可讓任何物件向定義附加屬性的類型報告資訊。An attached property is a property that enables any object to report information to the type that defines the attached property. WPFWPF中,繼承自 DependencyObject 的任何型別都可以使用附加屬性,不論型別是否繼承自訂屬性的型別。In WPFWPF, any type that inherits from DependencyObject can use an attached property regardless of whether the type inherits from the type that defines the property. 附加屬性是 XAMLXAML 語言的功能。An attached property is a feature of the XAMLXAML language. 若要在 XAMLXAML中設定附加屬性,請使用 [擁有宏]。propertyName語法。To set an attached property in XAMLXAML, use the ownerType.propertyName syntax. DockPanel.Dock 屬性是附加屬性的範例。An example of an attached property is the DockPanel.Dock property. 如果您想要建立可用於所有 DependencyObject 類型的屬性,則應該建立附加屬性。If you want to create a property that can be used on all DependencyObject types, then you should create an attached property. 若要深入瞭解附加屬性(包括如何建立),請參閱附加屬性總覽To learn more about attached properties, including how to create them, see Attached Properties Overview.

XAML Attribute UsageXAML Attribute Usage

<object property="dependencyPropertyName"/>  

-或--or-

<object property="ownerType.dependencyPropertyName"/>  

-或--or-

<object property="attachedPropertyOwnerType.attachedPropertyName"/>  

XAML 值XAML Values

dependencyPropertyName
字串,指定所需相依性屬性的 DependencyProperty.NameA string that specifies the DependencyProperty.Name of the desired dependency property. 如果屬性不在預設的 XML 命名空間中,這會在前面加上 XML 命名空間前置詞(如需詳細資訊,請參閱Xaml 命名空間和 WPF xaml 的命名空間對應)。This can be preceded by an XML namespace prefix if the property is not in the default XML namespace (for details, see XAML Namespaces and Namespace Mapping for WPF XAML.)

ownerType.dependencyPropertyNameownerType.dependencyPropertyName
字串,指定相依性屬性的擁有者類型、點(.),然後 DependencyProperty.NameA string that specifies an owner type of a dependency property, a dot (.), then the DependencyProperty.Name. ownerType 的前面也可以是 XML 命名空間前置詞。ownerType can also be preceded by an XML namespace prefix. 這是晚期繫結樣式和範本的特定用法,其中必須指定相依性屬性的擁有者來剖析內容,因為 TargetType 尚未知道。This usage is particular to late-bound styles and templates, where the owner of the dependency property must be specified for parsing context because the TargetType is not yet known. 如需詳細資訊,請參閱 設定樣式和範本For more information, see Styling and Templating.

attachedPropertyOwnerType 的雜湊碼。attachedPropertyOwnerType . attachedPropertyName
字串,指定附加屬性的擁有者、點(.),然後是附加屬性名稱。A string that specifies the owner of an attached property, a dot (.), then the attached property name. attachedPropertyOwnerType 的前面也可以是 XML 命名空間前置詞。attachedPropertyOwnerType can also be preceded by an XML namespace prefix.

欄位

UnsetValue

指定 WPFWPF 屬性系統所使用的靜態值,而非指定 null,以表示屬性已存在,但屬性系統尚未設定其值。Specifies a static value that is used by the WPFWPF property system rather than null to indicate that the property exists, but does not have its value set by the property system.

屬性

DefaultMetadata

取得相依性屬性的預設中繼資料。Gets the default metadata of the dependency property.

GlobalIndex

取得可唯一識別相依性屬性的內部產生值。Gets an internally generated value that uniquely identifies the dependency property.

Name

取得相依性屬性的名稱。Gets the name of the dependency property.

OwnerType

取得對屬性系統註冊相依性屬性之物件,或將本身作為屬性擁有者加入之物件的類型。Gets the type of the object that registered the dependency property with the property system, or added itself as owner of the property.

PropertyType

取得相依性屬性用於其值的型別。Gets the type that the dependency property uses for its value.

ReadOnly

取得值,這個值表示此 DependencyProperty 執行個體所識別的相依性屬性是否為唯讀相依性屬性。Gets a value that indicates whether the dependency property identified by this DependencyProperty instance is a read-only dependency property.

ValidateValueCallback

取得相依性屬性的值驗證回呼。Gets the value validation callback for the dependency property.

方法

AddOwner(Type)

加入其他類型,作為已註冊之相依性屬性的擁有者。Adds another type as an owner of a dependency property that has already been registered.

AddOwner(Type, PropertyMetadata)

加入另一個類型作為已註冊的相依性屬性的擁有者,並提供在所提供擁有者類型上存在之相依性屬性的相依性屬性中繼資料。Adds another type as an owner of a dependency property that has already been registered, providing dependency property metadata for the dependency property as it will exist on the provided owner type.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

傳回這個 DependencyProperty 的雜湊碼。Returns a hash code for this DependencyProperty.

GetMetadata(DependencyObject)

傳回這個相依性屬性的中繼資料,因為這個屬性位於指定的物件執行個體。Returns the metadata for this dependency property as it exists on the specified object instance.

GetMetadata(DependencyObjectType)

傳回存在於所指定類型之這個相依性屬性的中繼資料。Returns the metadata for this dependency property as it exists on a specified type.

GetMetadata(Type)

傳回這個相依性屬性的中繼資料,因為這個屬性位於指定的現有類型。Returns the metadata for this dependency property as it exists on a specified existing type.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsValidType(Object)

根據原始相依性屬性註冊所提供的屬性型別檢查這個相依性屬性的型別,判斷此型別是否接受指定的值。Determines whether a specified value is acceptable for this dependency property's type, as checked against the property type provided in the original dependency property registration.

IsValidValue(Object)

透過基本型別檢查,判斷屬性型別是否接受所提供的值,並在必要時判斷此值是否位於該型別的允許值範圍內。Determines whether the provided value is accepted for the type of property through basic type checking, and also potentially if it is within the allowed range of values for that type.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
OverrideMetadata(Type, PropertyMetadata)

當這個相依性屬性出現在所指定類型的執行個體中時,指定其替代中繼資料,並覆寫從基底類型繼承時存在的相依性屬性中繼資料。Specifies alternate metadata for this dependency property when it is present on instances of a specified type, overriding the metadata that existed for the dependency property as it was inherited from base types.

OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey)

當它出現在所指定類型的執行個體,覆寫在初始相依性屬性登錄中所提供的中繼資料時,提供唯讀相依性屬性的替代中繼資料。Supplies alternate metadata for a read-only dependency property when it is present on instances of a specified type, overriding the metadata that was provided in the initial dependency property registration. 您必須傳遞唯讀相依性屬性的 DependencyPropertyKey,以避免引發例外狀況。You must pass the DependencyPropertyKey for the read-only dependency property to avoid raising an exception.

Register(String, Type, Type)

使用指定的屬性名稱、屬性類型和擁有者類型以註冊相依性屬性。Registers a dependency property with the specified property name, property type, and owner type.

Register(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄相依性屬性。Registers a dependency property with the specified property name, property type, owner type, and property metadata.

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性名稱、屬性類型、擁有者類型、屬性中繼資料和屬性的值驗證回呼,註冊相依性屬性。Registers a dependency property with the specified property name, property type, owner type, property metadata, and a value validation callback for the property.

RegisterAttached(String, Type, Type)

使用指定的屬性名稱、屬性類型和擁有者類型以註冊附加屬性。Registers an attached property with the specified property name, property type, and owner type.

RegisterAttached(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄附加的屬性。Registers an attached property with the specified property name, property type, owner type, and property metadata.

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料以及屬性的值驗證回呼,註冊附加屬性。Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata)

註冊具有指定屬性類型、擁有者類型和屬性中繼資料的唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, and property metadata.

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼,註冊一個唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, property metadata, and a validation callback.

RegisterReadOnly(String, Type, Type, PropertyMetadata)

使用指定的屬性類型、擁有者類型和屬性中繼資料登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

ToString()

傳回相依性屬性的字串表示。Returns the string representation of the dependency property.

適用於

另請參閱