DependencyObject.CoerceValue(DependencyProperty) 方法

定義

強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。

public:
 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)

參數

dp
DependencyProperty

要強制轉型之相依性屬性的識別項。

例外狀況

指定的 dp 或其值無效或不存在。

範例

下列範例會在實作中 PropertyChangedCallback 呼叫 CoerceValue ,這個實作會做為 PropertyChangedCallback 相同類別上不同相依性屬性的 。 這是引進相依性屬性之間真實值相依性的常見模式。

private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  d.CoerceValue(MinReadingProperty);
  d.CoerceValue(MaxReadingProperty);
}
Private Shared Sub OnCurrentReadingChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    d.CoerceValue(MinReadingProperty)
    d.CoerceValue(MaxReadingProperty)
End Sub

備註

除了透過呼叫 CoerceValue 明確叫用之外, CoerceValueCallback 每當 WPF 屬性系統重新評估相依性屬性值時,也會在內部叫用相依性屬性的 。

當您叫 CoerceValue 用 方法時,最終會叫用您所指定屬性的強制值回呼。 通常只有在您知道強制值回呼存在時,才會叫 CoerceValue 用 ,而且如果您知道強制回應的回呼準則。

呼叫 CoerceValue 最常見的案例是在類別處理或屬性變更相關屬性的回呼內,以相依方式影響彼此的值。 如需詳細資訊,請參閱相依性屬性回呼和驗證

適用於

另請參閱