DependencyProperty.AddOwner 方法

定义

将另一种类型添加为已注册到一种类型的依赖属性的所有者。Adds another type as an owner of a dependency property that has already been registered to a type.

重载

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

参数

ownerType
Type

要作为此依赖属性所有者添加的类型。The type to add as an owner of this dependency property.

返回

一个引用,它指向可标识依赖属性的原始 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.

注解

使用此方法,属性系统可以在最初未注册特定依赖属性的类型上识别依赖属性。This method enables the property system to recognize a dependency property on a type that did not register that particular dependency property initially.

通常,AddOwner 用于将依赖项属性添加到未通过托管类继承公开该依赖属性的类(类继承将导致派生类继承包装属性,因此提供常规成员-对依赖属性的表访问权限已存在)。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

参数

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.

返回

一个引用,它指向可标识依赖属性的原始 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.

注解

使用此方法,属性系统可以在最初未注册特定依赖属性的类型上识别依赖属性。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 通过对原始所有者 Border 及其注册 BorderBrushProperty 依赖属性标识符调用 AddOwner,来定义属性系统的 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 调用本身永久保留,不必由 "添加所有者类标识符" 字段专门引用。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.

适用于