Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need for Coercing a Dependency Property?

I saw an example where there were 2 dependency properties:

public static readonly DependencyProperty CurrentReadingProperty = 
    DependencyProperty.Register("CurrentReading", 
    typeof(double), 
    typeof(Gauge), 
    new FrameworkPropertyMetadata(Double.NaN,
        FrameworkPropertyMetadataOptions.None,
        new PropertyChangedCallback(OnCurrentReadingChanged),
        new CoerceValueCallback(CoerceCurrentReading)
    ),
    new ValidateValueCallback(IsValidReading)
);

and

public static readonly DependencyProperty MinReadingProperty =
    DependencyProperty.Register(
    "MinReading",
    typeof(double),
    typeof(Gauge),
    new FrameworkPropertyMetadata(
        double.NaN,
        FrameworkPropertyMetadataOptions.None,
        new PropertyChangedCallback(OnMinReadingChanged),
        new CoerceValueCallback(CoerceMinReading)
    ),
    new ValidateValueCallback(IsValidReading));

and in OnCurrentReadingChanged I perform following operation d.CoerceValue(MinReadingProperty);

which invokes CoerceValueCallback delegate ("CoerceMinReading") which has following code:

private static object CoerceMinReading(DependencyObject d, object value)
{
    Gauge g = (Gauge)d;
    double min = (double)value;
    // some required conditions;
    return min;
}

What I want to understand is, why do I need to perform coercion?

Why can't I just call SetValue inside my property changed callback and change required properties instead of calling CoerceValue and handling things in my coerce callback?

like image 672
Rohan Gurjar Avatar asked May 21 '15 16:05

Rohan Gurjar


People also ask

Why do we need dependency properties?

The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs, such as: System properties, such as themes and user preference. Just-in-time property determination mechanisms, such as data binding and animations/storyboards.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.

What is the difference between property and dependency property?

The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object.

How does dependency property internally work?

A DependencyProperty maintains a static reference of all the DependencyProperty you register in WPF object hierarchy. It maintains a HashTable named PropertyFromName which it uses internally to get the DependencyProperty object. So in other word, each dependencyProperty object is registered in a global HashTable.


1 Answers

Coercion is designed to (optionally) make sure a value is valid in situations where it is alright for the UI layer to make such decisions. A classic example is some sort of slider control where a bound property is trying to set the value out of the specified range of the slider. In this case it is acceptable to 'clamp' the value ito it's minimum or maximum rather than throwing validation exceptions.

Calling SetValue during a SetValue property change is not efficient because you are potentially flooding the system with recursive events. This is why coercion exists. Just bear in mind its limits and use it where appropriate. In this case it is appropriate.

like image 52
James Lucas Avatar answered Sep 28 '22 05:09

James Lucas