संपादित करें

इसके माध्यम से साझा किया गया


DependencyProperty.RegisterReadOnly Method

Definition

Registers a dependency property as a read-only dependency property.

Overloads

RegisterReadOnly(String, Type, Type, PropertyMetadata)

Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

RegisterReadOnly(String, Type, Type, PropertyMetadata)

Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

public:
 static System::Windows::DependencyPropertyKey ^ RegisterReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata);
public static System.Windows.DependencyPropertyKey RegisterReadOnly (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata);
static member RegisterReadOnly : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterReadOnly (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata) As DependencyPropertyKey

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 key that should be used to set the value of a static read-only field in your class, which is then used to reference the dependency property.

Examples

The following example registers an AquariumSize dependency property as read-only. The example defines AquariumSizeKey as an internal key (so that other classes in the assembly could override metadata) and exposes the dependency property identifier based on that key as AquariumSizeProperty. Also, a wrapper is created for AquariumSize, with only a get accessor.

internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  "AquariumSize",
  typeof(double),
  typeof(Aquarium),
  new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
  AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
  get { return (double)GetValue(AquariumSizeProperty); }
}
Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
    Get
        Return CDbl(GetValue(AquariumSizeProperty))
    End Get
End Property

Remarks

This method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object). Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only dependency properties are a fairly typical scenario both in the existing API and for customization scenarios, because other WPF features might require a dependency property even if that property is not intended to be settable by callers. You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

For more information on dependency property registration, see DependencyProperty.

Applies to

RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

public:
 static System::Windows::DependencyPropertyKey ^ RegisterReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyPropertyKey RegisterReadOnly (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member RegisterReadOnly : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterReadOnly (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata, validateValueCallback As ValidateValueCallback) As DependencyPropertyKey

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 user-created callback that should perform any custom validation of the dependency property value beyond typical type validation.

Returns

A dependency property key that should be used to set the value of a static read-only field in your class, which is then used to reference the dependency property later.

Remarks

This method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object). Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only dependency properties are a fairly typical scenario. You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

For more information on dependency property registration, see DependencyProperty.

Validation on a read-only dependency property might be less important. The nonpublic access level you specify for the key reduces the likelihood for arbitrary invalid input.

Applies to