Editar

Compartir a través de


DependencyPropertyKey.OverrideMetadata(Type, PropertyMetadata) Method

Definition

Overrides the metadata of a read-only dependency property that is represented by this dependency property identifier.

public:
 void OverrideMetadata(Type ^ forType, System::Windows::PropertyMetadata ^ typeMetadata);
public void OverrideMetadata (Type forType, System.Windows.PropertyMetadata typeMetadata);
member this.OverrideMetadata : Type * System.Windows.PropertyMetadata -> unit
Public Sub OverrideMetadata (forType As Type, typeMetadata As PropertyMetadata)

Parameters

forType
Type

The type on which this dependency property exists and metadata should be overridden.

typeMetadata
PropertyMetadata

Metadata supplied for this type.

Exceptions

Attempted metadata override on a read-write dependency property (cannot be done using this signature).

Metadata was already established for the property as it exists on the provided type.

Examples

The following example overrides metadata for an existing read-only dependency property that a class inherits. In this case, the scenario goal was to add a coerce value callback that the base property metadata did not have. You could also override metadata for any of the other reasons that overriding metadata is typically appropriate (changing default value, adding FrameworkPropertyMetadataOptions values, etc.)

static Fishbowl() {
  Aquarium.AquariumSizeKey.OverrideMetadata(
    typeof(Aquarium),
    new PropertyMetadata(
      double.NaN,
      null,
      new CoerceValueCallback(CoerceFishbowlAquariumSize)
    )
  );
}

static object CoerceFishbowlAquariumSize(DependencyObject d,Object baseValue)
{
    //Aquarium is 2D, a Fishbowl is a round Aquarium, so the Size we return is the ellipse of that height/width rather than the rectangle
    Fishbowl fb = (Fishbowl)d;
    //other constraints assure that H,W are positive
    return Convert.ToInt32(Math.PI * (fb.Width / 2) * (fb.Height / 2));
}
Shared Sub New()
    Aquarium.AquariumSizeKey.OverrideMetadata(GetType(Aquarium), New PropertyMetadata(Double.NaN, Nothing, New CoerceValueCallback(AddressOf CoerceFishbowlAquariumSize)))
End Sub

Private Shared Function CoerceFishbowlAquariumSize(ByVal d As DependencyObject, ByVal baseValue As Object) As Object
    'Aquarium is 2D, a Fishbowl is a round Aquarium, so the Size we return is the ellipse of that height/width rather than the rectangle
    Dim fb As Fishbowl = CType(d, Fishbowl)
    'other constraints assure that H,W are positive
    Return Convert.ToInt32(Math.PI * (fb.Width / 2) * (fb.Height / 2))
End Function

Remarks

Overriding metadata on a read-only dependency property is done for similar reasons as overriding metadata on a read-write dependency property, and is restricted to access at the key level because behaviors specified in the metadata can change the set behavior (the default value, for instance).

As with read-write dependency properties, overriding metadata on a read-only dependency property should only be done prior to that property being placed in use by the property system (this equates to the time that specific instances of objects that register the property are instantiated). Calls to OverrideMetadata should only be performed within the static constructors of the type that provides itself as the forType parameter of this method, or equivalent initialization for that class.

This method effectively forwards to the OverrideMetadata method, passing the DependencyPropertyKey instance as the key parameter.

Applies to

See also