DependencyProperty.Register Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Registers a dependency property with the specified property name, property type, owner type, and property metadata for the property.

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

Syntax

'Declaration
Public Shared Function Register ( _
    name As String, _
    propertyType As Type, _
    ownerType As Type, _
    typeMetadata As PropertyMetadata _
) As DependencyProperty
public static DependencyProperty Register(
    string name,
    Type propertyType,
    Type ownerType,
    PropertyMetadata typeMetadata
)

Parameters

  • name
    Type: System.String
    The name of the dependency property to register.
  • propertyType
    Type: System.Type
    The type of the property.
  • ownerType
    Type: System.Type
    The owner type that is registering the dependency property.

Return Value

Type: System.Windows.DependencyProperty
A dependency property identifier that should be used to set the value of a public static readonly field in your class. The identifier is then used both by your own code and any third-party user code to reference the dependency property later, for operations such as setting its value programmatically, or attaching a Binding in code.

Exceptions

Exception Condition
ArgumentNullException

A required parameter was nulla null reference (Nothing in Visual Basic) (check the exception for the name of the missing parameter).

ArgumentException

A parameter was out of range, for instance name was an empty string.

-or-

Attempted to register with a propertyType that does not match a default value specified in the typeMetadata.

Remarks

Dependency property concepts are covered in detail in the topic Dependency Properties Overview.

For more information on using Register to register a custom dependency property, including examples and procedures, see Custom Dependency Objects and Dependency Properties.

If you do not need property metadata for your dependency property, specify typeMetadata as nulla null reference (Nothing in Visual Basic).

Silverlight does not support custom read-only dependency properties. For details, see Dependency Properties Overview.

Important noteImportant Note:

Do not register a dependency property with the specific default value of UnsetValue. This will be confusing for property consumers and will have unintended consequences within the property system.

Examples

The following example shows a basic usage where a DependencyProperty is established as a public static member of a class. This is done by calling Register and storing the return value.

Public Shared ReadOnly IsSpinningProperty As DependencyProperty = _
    DependencyProperty.Register("IsSpinning", _
    GetType(Boolean), _
    GetType(SilverlightExampleClass), _
    Nothing)
Public Property IsSpinning() As Boolean
    Get
        Return CBool(GetValue(IsSpinningProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(IsSpinningProperty, value)
    End Set
End Property
public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
    typeof(SilverlightExampleClass), null
    );
public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

You can find a similar example that provides more information about creating a custom dependency property in the topic Custom Dependency Objects and Dependency Properties.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.