DependencyObject.CoerceValue(DependencyProperty) 方法

定义

对指定依赖属性的值进行强制。Coerces the value of the specified dependency property. 通过对调用方 DependencyObject 上存在的依赖属性的属性元数据中所指定的任何 CoerceValueCallback 函数进行调用来完成此操作。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

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

要强制转换的依赖项属性的标识符。The identifier for the dependency property to coerce.

异常

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

示例

下面的示例在 @no__t 1 实现中调用 CoerceValue,该实现用作相同类上不同依赖属性的 @no__t。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)
{
  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 进行显式调用以外,每当 WPFWPF 属性系统重新计算依赖属性值时,也会在内部调用依赖属性的 CoerceValueCallbackIn 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. 通常,仅当知道强制值回调存在并且知道回调的强制条件时,才会调用 CoerceValueTypically 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.

适用于

另请参阅