Редагувати

Поділитися через


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

A reference to the original DependencyProperty identifier that identifies the dependency property. 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.

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 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. 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. 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. Otherwise, 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. 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. 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. You should use the DependencyProperty return value of the AddOwner method to define the dependency property identifier, and also to declare CLR property wrappers, for dependency properties that are added to types using AddOwner.

The AddOwner methodology recommended above is used when creating the dependency properties that are declared within WPF. For instance, both Border and Control define a BorderBrush dependency property, which have similar functionality. 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. 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.

Applies to

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

A reference to the original DependencyProperty identifier that identifies the dependency property. 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. It is good practice to expose the identifiers, as well as new CLR property wrappers, for dependency properties that are added to types using AddOwner.

The AddOwner methodology recommended above is used when creating APIs declared within WPF. For instance, both Border and Control define a BorderBrush dependency property, which have similar functionality. Control defines its BorderBrush property to the property system by calling AddOwner on original owner Border and its registered BorderBrushProperty dependency property identifer. 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.

The added owner's dependency property identifier should be used for operations such as GetValue. However, type-specific operations involving either types or instances of the class that wer added as owner with different metadata will still return the expected results. This is true even if the original (not the added owner's) dependency property identifier is specified in calls to methods such as GetValue or GetMetadata. The metadata for the added owner is perpetuated by the AddOwner call itself, not necessarily referenced exclusively by the adding owner class identifier field. Nevertheless, it is good practice to expose the identifier, as well as new CLR property wrappers, for dependency properties that are added to types using AddOwner, because failing to do so creates disparity between the CLR and XAML 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. Some characteristics, such as DefaultValue, are replaced if they are specified in the new metadata. Others, such as PropertyChangedCallback, are combined. 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 WPF dependency properties. For details, see Dependency Property Metadata and Framework Property Metadata.

Applies to