DependencyObject.InvalidateProperty(DependencyProperty) Method

Definition

Re-evaluates the effective value for the specified dependency property.

public:
 void InvalidateProperty(System::Windows::DependencyProperty ^ dp);
public void InvalidateProperty (System.Windows.DependencyProperty dp);
member this.InvalidateProperty : System.Windows.DependencyProperty -> unit
Public Sub InvalidateProperty (dp As DependencyProperty)

Parameters

dp
DependencyProperty

The DependencyProperty identifier of the property to invalidate.

Examples

The following example calls InvalidateProperty on a custom property, whenever properties that are involved in the invalidated property's calculations change. This is an alternative technique to calling the CoerceValue method, because invalidating the property will also call any registered CoerceValueCallback.

static AreaButton()
{
    WidthProperty.OverrideMetadata(typeof(AreaButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(InvalidateAreaProperty)));
    HeightProperty.OverrideMetadata(typeof(AreaButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(InvalidateAreaProperty)));
}
static void InvalidateAreaProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    d.InvalidateProperty(AreaProperty);
}
Shared Sub New()
    WidthProperty.OverrideMetadata(GetType(AreaButton), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf InvalidateAreaProperty)))
    HeightProperty.OverrideMetadata(GetType(AreaButton), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf InvalidateAreaProperty)))
End Sub
Private Shared Sub InvalidateAreaProperty(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    d.InvalidateProperty(AreaProperty)
End Sub

Remarks

When you call InvalidateProperty, any associated and applicable CoerceValueCallback or PropertyChangedCallback functions registered for that dependency property might be invoked.

Calling InvalidateProperty on a property that has its local value set will have no effect, because the local value takes precedence over other property system inputs, except for animations. However, you could call ClearValue, then call InvalidateProperty. For more information, see Dependency Property Value Precedence.

Calling InvalidateProperty is not necessarily applicable for many dependency property scenarios. If a dependency property becomes invalidated because of value changes in any of the constituents, the property system invalidates and re-evaluates the dependency property automatically. However, there are still some appropriate scenarios where InvalidateProperty is useful. In particular, you can use InvalidateProperty inside the coerce value or property changed callback for a different dependency property. You can also use InvalidateProperty to force re-evaluation of a binding against a data source that is not able to implement the recommended INotifyPropertyChanged notification mechanism (perhaps if consuming data classes that cannot be derived from, or where the data is a static member).

Applies to

See also