DependencyObject.InvalidateProperty(DependencyProperty) 方法

定義

重新評估指定相依性屬性的有效值。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 識別項。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 時,可能會叫用針對該相依性屬性註冊的任何相關聯且適用的 @no__t 1 或 @no__t 2 函數。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. 特別的是,您可以在強制轉型值或屬性變更的回呼中,針對不同的相依性屬性使用 InvalidatePropertyIn particular, you can use InvalidateProperty inside the coerce value or property changed callback for a different dependency property. 您也可以使用 InvalidateProperty,針對無法執行建議的 @no__t 1 通知機制(可能是使用無法衍生自的資料類別,或資料為靜態成員)的資料來源,強制重新評估系結。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).

適用於

另請參閱