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.
Public Shared Function Register( name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata ) As DependencyProperty
The name of the dependency property to register.
A property metadata instance. This can contain a PropertyChangedCallback implementation reference.
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.
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.
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.
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 Inherits Control Public Shared ReadOnly SpeciesProperty As DependencyProperty = _ DependencyProperty.Register( "Species", _ GetType(String), _ GetType(Fish), _ Nothing) Public Property Species As String Get Species = CType(GetValue(SpeciesProperty), String) End Get Set(value As String) SetValue(SpeciesProperty, value) End Set End Property End Class
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.
Minimum supported client
Minimum supported server
|Windows Server 2012|
Minimum supported phone
|Windows Phone 8.1 [Windows Runtime apps only]|
- Custom dependency properties
- Dependency properties overview
- XAML user and custom controls sample