Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 property on this type.

Namespace: System.Windows
Assembly: WindowsBase (in windowsbase.dll)

public void CoerceValue (
	DependencyProperty dp
)
public void CoerceValue (
	DependencyProperty dp
)
public function CoerceValue (
	dp : DependencyProperty
)
You cannot use methods in XAML.

Parameters

dp

The identifier for the dependency property to coerce.

Exception typeCondition

InvalidOperationException

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

The CoerceValueCallback is also invoked internally whenever a property value is being re-evaluated.

When you invoke the CoerceValue method, you are ultimately invoking the coerce value callback for the property that you specify. Typically you will only invoke CoerceValue if you know that a coerce value callback exists, and if you know the callback's criterion 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.

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);
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft