DependencyObject.CoerceValue(DependencyProperty) DependencyObject.CoerceValue(DependencyProperty) DependencyObject.CoerceValue(DependencyProperty) DependencyObject.CoerceValue(DependencyProperty) Method


強制轉型所指定相依性屬性的值。Coerces the value of the specified dependency property. 叫用存在於呼叫 CoerceValueCallback 上相依性屬性之屬性中繼資料內所指定的任何 DependencyObject 函式,就可以達到這一點。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

 void CoerceValue(System::Windows::DependencyProperty ^ dp);
public void CoerceValue (System.Windows.DependencyProperty dp);
member this.CoerceValue : System.Windows.DependencyProperty -> unit
Public Sub CoerceValue (dp As DependencyProperty)


DependencyProperty DependencyProperty DependencyProperty DependencyProperty

要強制轉型之相依性屬性的識別項。The identifier for the dependency property to coerce.


指定的 dp 或其值無效或不存在。The specified dp or its value were invalid or do not exist.


下列範例會呼叫CoerceValuePropertyChangedCallback實作,做為PropertyChangedCallback相同類別上不同的相依性屬性。The following example calls CoerceValue within a PropertyChangedCallback implementation that is used as the PropertyChangedCallback for a different dependency properties on the same class. 這是導入,則為 true 的值相依性屬性之間的相依性的常見模式。This is a common pattern for introducing true value dependencies between dependency properties.

private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Private Shared Sub OnCurrentReadingChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
End Sub


除了明確叫用透過呼叫CoerceValue,則CoerceValueCallback相依性屬性也會叫用內部每當相依性屬性值重新評估WPFWPF屬性系統。In addition to being explicitly invoked through calling CoerceValue, the CoerceValueCallback for a dependency property is also invoked internally whenever the dependency property value is being re-evaluated by the WPFWPF property system.

當您叫用CoerceValue方法中,您最終叫用您指定之屬性的強制轉型值回撥。When you invoke the CoerceValue method, you are ultimately invoking the coerce value callback for the property that you specify. 通常您會叫用CoerceValue唯有如果您知道的強制轉型值回呼的存在,而且如果您知道回呼的強制型轉的準則。Typically you will invoke CoerceValue only if you know that a coerce value callback exists, and if you know the callback's criteria for coercion.

最常見的案例呼叫CoerceValue內的相依的方式會影響其他的值的相關屬性的類別處理或屬性變更回呼。The most common scenario for calling CoerceValue is within class handling or property change callbacks of related properties that influence each other's values in a dependent way. 如需詳細資訊,請參閱相依性屬性回呼和驗證For more information, see Dependency Property Callbacks and Validation.