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方法,因为导致失效属性将同时调用任何注册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)
{
    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、 任何相关联的和适用CoerceValueCallbackPropertyChangedCallback函数注册可能会调用该依赖项属性。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. 但是,仍有一些适当的方案其中InvalidateProperty非常有用。However, 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).

适用于

另请参阅