Edit

Share via


CoerceValueCallback Delegate

Definition

Provides a template for a method that is called whenever a dependency property value is being re-evaluated, or coercion is specifically requested.

public delegate System::Object ^ CoerceValueCallback(DependencyObject ^ d, System::Object ^ baseValue);
public delegate object CoerceValueCallback(DependencyObject d, object baseValue);
type CoerceValueCallback = delegate of DependencyObject * obj -> obj
Public Delegate Function CoerceValueCallback(d As DependencyObject, baseValue As Object) As Object 

Parameters

d
DependencyObject

The object that the property exists on. When the callback is invoked, the property system will pass this value.

baseValue
Object

The new value of the property, prior to any coercion attempt.

Return Value

The coerced value (with appropriate type).

Examples

The following example includes an implementation of this callback to coerce the stored value of a dependency property based on other inputs, such as another property's value. In this case, the callback checks to see whether the ShirtType property corresponds to a type of shirt that has buttons; if so it establishes a starting default color for the ButtonColor, if the shirt type has no buttons, it coerces the ButtonColor value back to a starting value, which causes the UI (not shown) to remove that dropdown from the effective choices.

private static object CoerceButtonColor(DependencyObject d, object value)
{
    ShirtTypes newShirtType = (d as Shirt).ShirtType;
    if (newShirtType == ShirtTypes.Dress || newShirtType == ShirtTypes.Bowling)
    {
        return ButtonColors.Black;				
    }
    return ButtonColors.None;
}
Private Shared Function CoerceButtonColor(ByVal d As DependencyObject, ByVal value As Object) As Object
    Dim newShirtType As ShirtTypes = (TryCast(d, Shirt)).ShirtType
    If newShirtType = ShirtTypes.Dress OrElse newShirtType = ShirtTypes.Bowling Then
        Return ButtonColors.Black
    End If
    Return ButtonColors.None
End Function

Remarks

Callbacks based on CoerceValueCallback can be assigned to a dependency property through several different techniques. Each of these techniques requires that you first create a new property metadata object (PropertyMetadata, or a derived class such as FrameworkPropertyMetadata). Create the metadata object using a constructor signature that takes the coerceValueCallback parameter, and assign that parameter to your callback handler. Or construct the metadata by any signature and set the CoerceValueCallback property prior to putting the metadata in use.

When you have this metadata, you can:

Implementations of this callback should check the value in baseValue and determine based on either the value or the type whether this is a value that needs to be further coerced.

The CoerceValueCallback for a dependency property is invoked any time that the property system or any other caller calls CoerceValue on a DependencyObject instance, specifying that property's identifier as the dp.

Changes to the property value may have come from any possible participant in the property system. This includes styles, generic invalidation, triggers, property value inheritance, and local value setting.

Generally you should avoid specifying more than one CoerceValueCallback for any given dependency property (overriding or adding with new metadata for a dependency property that already had a CoerceValueCallback). Only one of the callbacks will be able to act. The acting callback will be the one that was applied to the most derived class in the inheritance as compared to the DependencyObject caller. Other callbacks as assigned to metadata for the dependency property as it existed higher in the owner hierarchy are replaced when the metadata is overridden.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

See also