Clément Plantec Clément Plantec - 18 days ago 5
C# Question

DependencyProperty CoerceValueCallback with generic

I'm building a wpf control that use DependencyProperty with generic, for which I need (at least I think) a CoerceValueCallback to check if the value is correct.
The idea is to build a base class from which I'll derive with number type.

public class MyClass<T> : Control where T : struct
{

public T Value
{
get { return (T)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}

// DependencyProperty as the backing store for Value
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value",
typeof(T),
typeof(MyClass<T>),
new PropertyMetadata(null, null, CoerceValue)
);


private static object CoerceValue(DependencyObject d, object baseValue)
{
// Check if value is valid
return verifiedValue;
}
}


public class MyDerivedClass : MyClass<int>
{

}


Problem is that the CoerceValue is returning an object and I can't find how to return a generic instead.

Any idea ?

Answer

While your control can utilize the generic, when using the CoerceValueCallback, you will have to maintain the delegate callback. The C# documentation specifies that delegates are sealed.

I also noticed you have an issue with your PropertyMetadata. You are passing null as the default value. Which is what caught me off guard when I previously checked for null. Here is the code that worked for me when I tested it:

public class MyClass<T> : Control where T : struct
{
    public T Value
    {
        get { return (T)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    // DependencyProperty as the backing store for Value
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value",
        typeof(T),
        typeof(MyClass<T>),
        new PropertyMetadata(default(T), null, CoerceValue) //Must pass default(T) otherwise an exception will occur.
    );

    private static object CoerceValue(DependencyObject d, object baseValue)
    {
        var tValue = (T)baseValue;
        // Check if tValue is valid 
        return tValue;
    }
}

Once you have the value returned from the CoerceValue method, you will have to cast it to T.