DependencyProperty DependencyProperty DependencyProperty DependencyProperty Class

定義

代表可以透過方法設定的屬性,例如樣式、資料繫結、動畫和繼承。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
繼承
DependencyPropertyDependencyPropertyDependencyPropertyDependencyProperty
屬性

備註

ADependencyProperty支援中的下列功能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. 如需有關資料繫結相依性屬性的詳細資訊,請參閱How to:將兩個控制項的屬性繫結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,例如變更屬性值是否是否需要重新撰寫項目的視覺效果的版面配置系統。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 StudioThe 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,使用ownerTypepropertyName語法。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.Name所需的相依性屬性。A 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 UnsetValue UnsetValue 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 DefaultMetadata DefaultMetadata DefaultMetadata

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

GlobalIndex GlobalIndex GlobalIndex GlobalIndex

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

Name Name Name Name

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

OwnerType OwnerType OwnerType 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 PropertyType PropertyType PropertyType

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

ReadOnly ReadOnly ReadOnly ReadOnly

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

ValidateValueCallback ValidateValueCallback ValidateValueCallback ValidateValueCallback

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

方法

AddOwner(Type) AddOwner(Type) AddOwner(Type) AddOwner(Type)

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

AddOwner(Type, PropertyMetadata) AddOwner(Type, PropertyMetadata) AddOwner(Type, PropertyMetadata) 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) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

GetMetadata(DependencyObject) GetMetadata(DependencyObject) GetMetadata(DependencyObject) GetMetadata(DependencyObject)

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

GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType)

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

GetMetadata(Type) GetMetadata(Type) GetMetadata(Type) GetMetadata(Type)

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsValidType(Object) IsValidType(Object) IsValidType(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) IsValidValue(Object) IsValidValue(Object) 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() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
OverrideMetadata(Type, PropertyMetadata) OverrideMetadata(Type, PropertyMetadata) OverrideMetadata(Type, PropertyMetadata) 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) OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey) OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey) 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) Register(String, Type, Type) Register(String, Type, Type) Register(String, Type, Type)

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

Register(String, Type, Type, PropertyMetadata) Register(String, Type, Type, PropertyMetadata) Register(String, Type, Type, PropertyMetadata) 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) Register(String, Type, Type, PropertyMetadata, ValidateValueCallback) Register(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type)

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

RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) 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) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata) 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) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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) RegisterReadOnly(String, Type, Type, PropertyMetadata) RegisterReadOnly(String, Type, Type, PropertyMetadata) 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) RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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() ToString() ToString() ToString()

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

適用於

另請參閱