แก้ไข

แชร์ผ่าน


DependencyPropertyKey.DependencyProperty Property

Definition

Gets the dependency property identifier associated with this specialized read-only dependency property identifier.

public:
 property System::Windows::DependencyProperty ^ DependencyProperty { System::Windows::DependencyProperty ^ get(); };
public System.Windows.DependencyProperty DependencyProperty { get; }
member this.DependencyProperty : System.Windows.DependencyProperty
Public ReadOnly Property DependencyProperty As DependencyProperty

Property Value

The relevant dependency property identifier.

Examples

The following example calls DependencyProperty to expose the DependencyProperty identifier (AquariumGraphicProperty) for the AquariumGraphic read-only dependency property on a class. The example also shows the DependencyPropertyKey creation (as an internal member) and the get accessor for AquariumGraphic.

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

The DependencyProperty value enables a read-only property's identifier to participate in common property system operations using some of the same interfaces as used for read-write dependency properties.

In order to implement the get property accessor for a read-only dependency property, you should create and expose a DependencyProperty identifier on your class. This serves two purposes:

  • Your own class needs the DependencyProperty identifier in order to implement the get accessor for the property wrapper. You use the DependencyProperty as a parameter for the GetValue call that implements the get accessor.

  • DependencyProperty identifiers expose your dependency property to the property system such that other methods that rely on metadata can access it in a standard form. For instance, if you called GetLocalValueEnumerator on some DependencyObject and obtained an enumeration of locally set properties (values and identifiers) the identifier returned for a read-only dependency property would be your DependencyProperty value rather than the key. Not exposing a DependencyProperty identifier does not increase the security of your read-only dependency property in any way, it just makes operations that involve your property more awkward both for subsequent derived classes and class instances.

To expose the DependencyProperty identifier on your class, you call DependencyProperty directly on your key. Use this value to create a public static readonly DependencyProperty identifier on the class, which parallels the DependencyPropertyKey.

Applies to

See also