Export (0) Print
Expand All

DependencyProperty.AddOwner Method (Type)

Adds another type as an owner of a dependency property that has already been registered.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

'Declaration
Public Function AddOwner ( _
	ownerType As Type _
) As DependencyProperty

Parameters

ownerType
Type: System.Type

The type to add as an owner of this dependency property.

Return Value

Type: System.Windows.DependencyProperty
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.

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 Nothing. 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.

This example shows how to add a class as an owner of a dependency property registered for a different type. By doing this, the WPF XAML reader and property system are both able to recognize the class as an additional owner of the property. Adding as owner optionally allows the adding class to provide type-specific metadata.

In the following example, StateProperty is a property registered by the MyStateControl class. The class UnrelatedStateControl adds itself as an owner of the StateProperty using the AddOwner method, specifically using the signature that allows for new metadata for the dependency property as it exists on the adding type. Notice that you should provide common language runtime (CLR) accessors for the property similar to the example shown in the How to: Implement a Dependency Property example, as well as re-expose the dependency property identifier on the class being added as owner.

Without wrappers, the dependency property would still work from the perspective of programmatic access using GetValue or SetValue. But you typically want to parallel this property-system behavior with the CLR property wrappers. The wrappers make it easier to set the dependency property programmatically, and make it possible to set the properties as XAML attributes.

To find out how to override default metadata, see How to: Override Metadata for a Dependency Property.

  Public Class MyStateControl
	  Inherits ButtonBase
	Public Sub New()
		MyBase.New()
	End Sub
	Public Property State() As Boolean
	  Get
		  Return CType(Me.GetValue(StateProperty), Boolean)
	  End Get
	  Set(ByVal value As Boolean)
		  Me.SetValue(StateProperty, value)
	  End Set
	End Property
	Public Shared ReadOnly StateProperty As DependencyProperty = DependencyProperty.Register("State", GetType(Boolean), GetType(MyStateControl),New PropertyMetadata(False))
  End Class


...


  Public Class UnrelatedStateControl
	  Inherits Control
	Public Sub New()
	End Sub
	Public Shared ReadOnly StateProperty As DependencyProperty = MyStateControl.StateProperty.AddOwner(GetType(UnrelatedStateControl), New PropertyMetadata(True))
	Public Property State() As Boolean
	  Get
		  Return CType(Me.GetValue(StateProperty), Boolean)
	  End Get
	  Set(ByVal value As Boolean)
		  Me.SetValue(StateProperty, value)
	  End Set
	End Property
  End Class

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft