Udostępnij za pośrednictwem


DependencyProperty.Register Method

Definition

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.

 static DependencyProperty Register(winrt::hstring const& name, TypeName const& propertyType, TypeName const& ownerType, PropertyMetadata const& typeMetadata);
public static DependencyProperty Register(string name, System.Type propertyType, System.Type ownerType, PropertyMetadata typeMetadata);
function register(name, propertyType, ownerType, typeMetadata)
Public Shared Function Register (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata) As DependencyProperty

Parameters

name
String

winrt::hstring

The name of the dependency property to register.

propertyType
TypeName Type

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

ownerType
TypeName Type

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

typeMetadata
PropertyMetadata

A property metadata instance. This can contain a default value and a PropertyChangedCallback implementation reference. Passing null for this parameter is equivalent to passing a new PropertyMetadata instance created by calling PropertyMetadata.Create with null as the default value parameter.

Returns

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 Visual C++ component extensions (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 Visual C++ component extensions (C++/CX), see Custom dependency properties.

Applies to

See also