DependencyProperty.Register method

Applies to Windows and Windows Phone

Registers a dependency property with the specified property name, property type, owner type, and property metadata for the property. Use this method when defining or initializing a DependencyObject derived class that will own the registered dependency property.

Syntax


public static DependencyProperty Register(
  string name, 
  Type propertyType, 
  Type ownerType, 
  PropertyMetadata typeMetadata
)

Parameters

name

Type: System.String [.NET] | Platform::String [C++]

The name of the dependency property to register.

propertyType

Type: System.Type [.NET] | TypeName [C++]

The type of the property, as a type reference (System.Type for .NET, a TypeName helper struct for C++/CX).

ownerType

Type: System.Type [.NET] | TypeName [C++]

The owner type that is registering the dependency property, as a type reference (System.Type for .NET, a TypeName helper struct for C++/CX).

typeMetadata

Type: PropertyMetadata

A property metadata instance. This can contain a PropertyChangedCallback implementation reference.

Return value

Type: DependencyProperty

A dependency property identifier that typically is stored in a public static read-only field in your DependencyObject derived 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.

Remarks

How to register a custom dependency property is described in detail (with examples) in the topic Custom dependency properties.

Registering a dependency property is typically something that you only do when an app first starts or DependencyObject derived classes defined by your app code are first used. You need to register the dependency properties early in the app lifetime to assure that other code and XAML in your app don't try to use the dependency properties before they are registered and available. Exactly how and when to register varies, based on the programming language. For C# or Microsoft Visual Basic it's common to register dependency properties during the static class initialization of the class that owns the dependency property. That way any code or XAML that initializes the instance will invoke that static construction and register the dependency property. For C++/CX), static class initialization isn't an available technique, so you typically must define an app-wide helper method that registers all the custom dependency properties that your app intends to use, as part of the Application object initialization. For examples of how and when to register custom dependency properties, including some special techniques that are needed for C++/CX, see Custom dependency properties.

Tip  

If you are programming using a .NET language (C# or Visual Basic), the TypeName type projects as System.Type. When programming using C#, it is common to use the typeof operator to get references to the System.Type of a type. In Visual Basic, use GetType. If you're using C++/CX), where you'll need to create a TypeName helper struct, you can use the typeid component extension.

Examples

This example shows a basic usage where a DependencyProperty is established as a public static member of a class. This is done by calling DependencyProperty.Register and storing the return value. For more examples, including a C++/CX example, see Custom dependency properties.


public class Fish : Control
{
    public static readonly DependencyProperty SpeciesProperty =
    DependencyProperty.Register(
    "Species",
    typeof(String),
    typeof(Fish), null
    );
    public string Species
    {
        get { return (string)GetValue(SpeciesProperty); }
        set { SetValue(SpeciesProperty, (string)value); }
    }
}


Note  A C++/CX example isn't shown here because it must be factored completely differently than the .NET examples, and involves several different code files. See examples in Custom dependency properties.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

DependencyProperty
Custom dependency properties
Dependency properties overview
XAML user and custom controls sample

 

 

Show:
© 2014 Microsoft