DependencyObject.InvalidateProperty(DependencyProperty) DependencyObject.InvalidateProperty(DependencyProperty) DependencyObject.InvalidateProperty(DependencyProperty) DependencyObject.InvalidateProperty(DependencyProperty) Method

定义

重新计算指定依赖项属性的有效值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)

参数

dp
DependencyProperty DependencyProperty DependencyProperty 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方法的替代方法, 因为使属性失效还会调用任何已注册CoerceValueCallback的。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

注解

调用InvalidateProperty时, 可能会调用为该CoerceValueCallback依赖属性注册的任何关联的和适用的或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).

适用于

另请参阅