DependencyObject.InvalidateProperty(DependencyProperty) 方法


重新评估指定依赖属性的有效值。Re-evaluates the effective value for the specified dependency property.

 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)



要使其失效的属性的 DependencyProperty 标识符。The DependencyProperty identifier of the property to invalidate.


下面的示例 InvalidateProperty 在自定义属性上调用,而在属性的计算中涉及的属性发生变化时。The following example calls InvalidateProperty on a custom property, whenever properties that are involved in the invalidated property's calculations change. 这是一种调用方法的替代方法 CoerceValue ,因为使属性失效还会调用任何已注册的 CoerceValueCallbackThis 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)
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)
End Sub


调用时 InvalidatePropertyCoerceValueCallback 可能会 PropertyChangedCallback 调用为该依赖属性注册的任何关联的和适用的或函数。When you call InvalidateProperty, any associated and applicable CoerceValueCallback or PropertyChangedCallback functions registered for that dependency property might be invoked.

InvalidateProperty对设置了本地值的属性调用将不起作用,因为本地值优先于其他属性系统输入,动画除外。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. 但是,您可以调用 ClearValue ,然后调用 InvalidatePropertyHowever, you could call ClearValue, then call InvalidateProperty. 有关详细信息,请参阅依赖属性值优先级For more information, see Dependency Property Value Precedence.

调用 InvalidateProperty 不一定适用于许多依赖属性方案。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. 但仍有一些适合的方案 InvalidatePropertyHowever, there are still some appropriate scenarios where InvalidateProperty is useful. 特别是,可以在 InvalidateProperty 其他依赖属性的强制值或属性更改回调内使用。In particular, you can use InvalidateProperty inside the coerce value or property changed callback for a different dependency property. 你还可以使用 InvalidateProperty 来对无法实现建议的通知机制的数据源 INotifyPropertyChanged (如果使用不能从派生的数据类)或数据是静态成员) 的数据源强制重新计算绑定 (。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).