DependencyProperty.Register Method

Definition

Registers a dependency property.

Overloads

Register(String, Type, Type)

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

Register(String, Type, Type, PropertyMetadata)

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

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

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

Register(String, Type, Type)

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

C#
public static System.Windows.DependencyProperty Register(string name, Type propertyType, Type ownerType);

Parameters

name
String

The name of the dependency property to register. The name must be unique within the registration namespace of the owner type.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

Returns

A dependency property identifier that should be used to set the value of a public static readonly field in your class. That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Examples

C#
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
  "IsDirty",
  typeof(Boolean),
  typeof(AquariumObject3)
);

Remarks

For more information on dependency property registration, see DependencyProperty.

See also

Applies to

.NET Framework 4.8.1 et autres versions
Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Register(String, Type, Type, PropertyMetadata)

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

C#
public static System.Windows.DependencyProperty Register(string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata);

Parameters

name
String

The name of the dependency property to register.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

typeMetadata
PropertyMetadata

Property metadata for the dependency property.

Returns

A dependency property identifier that should be used to set the value of a public static readonly field in your class. That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Remarks

For more information on dependency property registration, see DependencyProperty.

See also

Applies to

.NET Framework 4.8.1 et autres versions
Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

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

C#
public static System.Windows.DependencyProperty Register(string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata, System.Windows.ValidateValueCallback validateValueCallback);

Parameters

name
String

The name of the dependency property to register.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

typeMetadata
PropertyMetadata

Property metadata for the dependency property.

validateValueCallback
ValidateValueCallback

A reference to a callback that should perform any custom validation of the dependency property value beyond typical type validation.

Returns

A dependency property identifier that should be used to set the value of a public static readonly field in your class. That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Examples

The following example registers a dependency property, including a validation callback (the callback definition is not shown; for details on the callback definition, see ValidateValueCallback).

C#
public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(
    "CurrentReading",
    typeof(double),
    typeof(Gauge),
    new FrameworkPropertyMetadata(
        Double.NaN,
        FrameworkPropertyMetadataOptions.AffectsMeasure,
        new PropertyChangedCallback(OnCurrentReadingChanged),
        new CoerceValueCallback(CoerceCurrentReading)
    ),
    new ValidateValueCallback(IsValidReading)
);
public double CurrentReading
{
  get { return (double)GetValue(CurrentReadingProperty); }
  set { SetValue(CurrentReadingProperty, value); }
}

Remarks

For more information on dependency property registration, see DependencyProperty.

See also

Applies to

.NET Framework 4.8.1 et autres versions
Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10