DependencyProperty.AddOwner Method

Definition

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

Overloads

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.

AddOwner(Type)

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

public:
 System::Windows::DependencyProperty ^ AddOwner(Type ^ ownerType);
public System.Windows.DependencyProperty AddOwner (Type ownerType);
member this.AddOwner : Type -> System.Windows.DependencyProperty
Public Function AddOwner (ownerType As Type) As DependencyProperty

Parameters

ownerType
Type

要加入為此相依性屬性之擁有者的類型。The type to add as an owner of this dependency property.

Returns

DependencyProperty

原始 DependencyProperty 識別項的參考,它識別相依性屬性。A reference to the original DependencyProperty identifier that identifies the dependency property. 應該藉由加入類別作為 public static readonly 欄位來公開此識別項。This identifier should be exposed by the adding class as a public static readonly field.

Remarks

這個方法可讓屬性系統在一開始不會註冊該特定相依性屬性的類型上辨識相依性屬性。This method enables the property system to recognize a dependency property on a type that did not register that particular dependency property initially.

一般來說,AddOwner 是用來將相依性屬性新增至尚未透過 managed 類別繼承公開該相依性屬性的類別(類別繼承會造成衍生類別繼承包裝函式屬性,因此會提供一般成員資料表對相依性屬性的存取權)。Typically, AddOwner is used to add dependency properties to classes that do not already expose that dependency property through managed class inheritance (class inheritance would cause the wrapper properties to be inherited by the derived class, and thus would provide general members-table access to the dependency property already). AddOwner 可以讓屬性系統在一開始不註冊該相依性屬性的型別上辨識相依性屬性。AddOwner enables the property system to recognize a dependency property on a type that did not register that dependency property initially.

此簽章不允許指定中繼資料。This signature does not allow for specifying metadata. 當您使用這個方法時,會自動為新的 DependencyProperty 及其擁有者類型產生中繼資料。When you use this method, the metadata is automatically generated for the new DependencyProperty and its owner type. 自動產生的中繼資料是來自已定義此屬性之所有基底類型中合併中繼資料的結果。The auto-generated metadata is the result of the merged metadata from all of the base types that have this property defined. 如果沒有合併的中繼資料可用,則會使用屬性的預設中繼資料。If no merged metadata is available, then the default metadata for the property is used. 如果使用 RegisterAttached 方法來註冊屬性,則預設的中繼資料會與呼叫 RegisterAttached 時所建立的中繼資料相同。If the property is registered by using the RegisterAttached method, then the default metadata is the same as the metadata that is created when RegisterAttached was called. 否則,會建立 PropertyMetadata 物件,並將 DefaultValue 屬性設定為屬性類型的預設值,而且 PropertyMetadata 的所有其他屬性都設定為 nullOtherwise, the PropertyMetadata object is created with the DefaultValue property set to the property type's default and all other properties of the PropertyMetadata is set to null. 如果您想要提供相依性屬性版本的中繼資料(加入至提供的類型),請使用 AddOwner(Type, PropertyMetadata) 簽章。Use the AddOwner(Type, PropertyMetadata) signature if you want to provide metadata for the version of the dependency property as added to the provided type.

這個方法的傳回值通常是用來透過儲存相依性屬性識別碼來宣告和公開相依性屬性。The return value of this method is typically used to declare and expose the dependency property by storing a dependency property identifier. 如果您想要針對相依性屬性呼叫屬性系統 Api,此識別碼會提供相依性屬性的存取權,特別是存在於新增擁有者類別上。The identifier provides access to the dependency property if you want to call property system APIs against the dependency property, particularly as it exists on the adding owner class. 原始擁有者和加入的擁有者都應該使用相同的屬性名稱來表示類似的功能。The same property name for both original owner and added owner should be used to indicate the similar functionality. 您應該使用 AddOwner 方法的 DependencyProperty 傳回值來定義相依性屬性識別碼,也可以針對使用 AddOwner新增至類型的相依性屬性,宣告 CLRCLR 屬性包裝函式。You should use the DependencyProperty return value of the AddOwner method to define the dependency property identifier, and also to declare CLRCLR property wrappers, for dependency properties that are added to types using AddOwner.

建立在 WPFWPF內宣告的相依性屬性時,會使用上述建議的 AddOwner 方法。The AddOwner methodology recommended above is used when creating the dependency properties that are declared within WPFWPF. 例如,BorderControl 會定義具有類似功能的 BorderBrush 相依性屬性。For instance, both Border and Control define a BorderBrush dependency property, which have similar functionality. Control 會根據原始擁有者 Border 和其註冊的 BorderBrushProperty 相依性屬性識別碼,呼叫 AddOwner,將其 BorderBrush 屬性定義為屬性系統。Control defines its BorderBrush property to the property system by calling AddOwner based on the original owner Border and its registered BorderBrushProperty dependency property identifer. 然後,AddOwner 傳回值會用來在新增的擁有者上建立該屬性的新靜態 DependencyProperty 欄位(BorderBrushProperty),而且也會宣告 BorderBrush 屬性包裝函式。The AddOwner return value is then used to establish a new static DependencyProperty field (BorderBrushProperty) for that property on the added owner, and a BorderBrush property wrapper is also declared.

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.

public:
 System::Windows::DependencyProperty ^ AddOwner(Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata);
public System.Windows.DependencyProperty AddOwner (Type ownerType, System.Windows.PropertyMetadata typeMetadata);
member this.AddOwner : Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
Public Function AddOwner (ownerType As Type, typeMetadata As PropertyMetadata) As DependencyProperty

Parameters

ownerType
Type

要加入為此相依性屬性之擁有者的類型。The type to add as owner of this dependency property.

typeMetadata
PropertyMetadata

限定存在於所提供類型之相依性屬性的中繼資料。The metadata that qualifies the dependency property as it exists on the provided type.

Returns

DependencyProperty

原始 DependencyProperty 識別項的參考,它識別相依性屬性。A reference to the original DependencyProperty identifier that identifies the dependency property. 應該藉由加入類別作為 public static readonly 欄位來公開此識別項。This identifier should be exposed by the adding class as a public static readonly field.

Remarks

這個方法可讓屬性系統在一開始不會註冊該特定相依性屬性的類型上辨識相依性屬性。This method enables the property system to recognize a dependency property on a type that did not register that particular dependency property initially.

這個方法的傳回值是用來宣告和公開相依性屬性,特別是它存在於新增擁有者類別上。The return value of this method is used to declare and expose the dependency property, particularly as it exists on the adding owner class. 一般來說,原始擁有者和加入的擁有者都應該使用相同的屬性名稱來表示類似的功能。Generally, the same property name for both original owner and added owner should be used to indicate the similar functionality. 使用 AddOwner,針對加入至類型的相依性屬性公開識別碼,以及新的 CLRCLR 屬性包裝函式是很好的作法。It is good practice to expose the identifiers, as well as new CLRCLR property wrappers, for dependency properties that are added to types using AddOwner.

建立在 WPFWPF內宣告的 Api 時,會使用上述建議的 AddOwner 方法。The AddOwner methodology recommended above is used when creating APIs declared within WPFWPF. 例如,BorderControl 會定義具有類似功能的 BorderBrush 相依性屬性。For instance, both Border and Control define a BorderBrush dependency property, which have similar functionality. Control 會藉由呼叫原始擁有者 BorderAddOwner 及其註冊的 BorderBrushProperty 相依性屬性識別碼,將其 BorderBrush 屬性定義為屬性系統。Control defines its BorderBrush property to the property system by calling AddOwner on original owner Border and its registered BorderBrushProperty dependency property identifer. 然後,AddOwner 傳回值會用來在新增的擁有者上建立該屬性的靜態 DependencyProperty 欄位(BorderBrushProperty),而且也會宣告 BorderBrush 屬性包裝函式。The AddOwner return value is then used to establish a static DependencyProperty field (BorderBrushProperty) for that property on the added owner, and a BorderBrush property wrapper is also declared.

新增的擁有者的相依性屬性識別碼應用於 GetValue之類的作業。The added owner's dependency property identifier should be used for operations such as GetValue. 不過,涉及已加入為具有不同中繼資料之擁有者的類型或實例的類型特定作業,仍然會傳回預期的結果,即使原始(而非新增的擁有者)相依性屬性識別碼是在呼叫 GetValueGetMetadata等方法時指定。However, type-specific operations involving either types or instances of the class that was added as owner with different metadata will still return the expected results even if the original (not the added owner's) dependency property identifier is specified in calls to methods such as GetValue or GetMetadata. 新增之擁有者的中繼資料是由 AddOwner 呼叫本身所 perpetuated,不一定是由 [新增擁有者類別識別碼] 欄位獨佔參考。The metadata for the added owner is perpetuated by the AddOwner call itself, not necessarily referenced exclusively by the adding owner class identifier field. 儘管如此,針對使用 AddOwner加入型別的相依性屬性公開識別碼,以及新的 CLRCLR 屬性包裝函式是個不錯的作法,因為這樣做可能會在屬性的 CLRCLRXAMLXAML 標記法之間建立差異。Nevertheless, it is good practice to expose the identifier, as well as new CLRCLR property wrappers, for dependency properties that are added to types using AddOwner, because failing to do so creates disparity between the CLRCLR and XAMLXAML representations of your properties.

提供的中繼資料會與相依性屬性的屬性中繼資料合併,因為它存在於基底擁有者上。The supplied metadata is merged with the property metadata for the dependency property as it exists on the base owner. 原始基底中繼資料中指定的任何特性都會保存。Any characteristics that were specified in the original base metadata will persist. 只有在新的中繼資料中特別變更的特性,才會覆寫基底中繼資料的特性。Only those characteristics that were specifically changed in the new metadata will override the characteristics of the base metadata. 某些特性(例如 DefaultValue)會在新的中繼資料中指定時予以取代。Some characteristics, such as DefaultValue, are replaced if they are specified in the new metadata. 其他專案(例如 PropertyChangedCallback)則結合在一起。Others, such as PropertyChangedCallback, are combined. 最後,合併行為取決於用於覆寫的屬性元資料類型,因此這裡所述的行為適用于 WPFWPF 相依性屬性所使用的現有屬性中繼資料類別。Ultimately, the merge behavior depends on the property metadata type being used for the override, so the behavior described here is for the existing property metadata classes used by WPFWPF dependency properties. 如需詳細資訊,請參閱相依性屬性中繼資料架構屬性中繼資料For details, see Dependency Property Metadata and Framework Property Metadata.

Applies to