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時, 可能會叫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. 不過, 仍有一些適合的案例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).

適用於

另請參閱