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

定义

对指定依赖属性的值进行强制。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 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)
{
  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依赖项属性也会调用在内部由重新计算依赖项属性值时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.

适用于

另请参阅