Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

DependencyObject.CoerceValue Method

Coerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
public void CoerceValue(
	DependencyProperty dp
)

Parameters

dp
Type: System.Windows.DependencyProperty
The identifier for the dependency property to coerce.
ExceptionCondition
InvalidOperationException

The specified dp or its value were invalid or do not exist.

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 WPF property system.

When you invoke the CoerceValue method, you are ultimately invoking the coerce value callback for the property that you specify. 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.

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.

The following example calls CoerceValue within a PropertyChangedCallback implementation that is used as the PropertyChangedCallback for a different dependency properties on the same class. 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);
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.