DependencyProperty CoerceValueCallback with generic
<p>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.</p>

<pre><code>public class MyClass&lt;T&gt; : 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&lt;T&gt;),
        new PropertyMetadata(null, null, CoerceValue)
    );


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


public class MyDerivedClass : MyClass&lt;int&gt;
{

}
</code></pre>

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

<p>Any idea ?</p>

<p><strong>EDIT:</strong> Here is what I did thanks to the answers below</p>

<pre><code>public abstract class MyClass&lt;T&gt; : Control where T : struct, IComparable
{

    public T MinValue { get; set; }
    public T MaxValue { get; set; }

    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&lt;T&gt;),
        new PropertyMetadata(default(T), null, CoerceValue)
    );


    private static object CoerceValue(DependencyObject d, object baseValue)
    {
        T value = (T)baseValue;
        ((MyClass&lt;T&gt;)d).CoerceValueToBounds(ref value);

        return value;
    }

    private void CoerceValueToBounds(ref T value)
    {
        if (value.CompareTo(MinValue) &lt; 0)
            value = MinValue;
        else if (value.CompareTo(MaxValue) &gt; 0)
            value = MaxValue;
    }
}
</code></pre>

<p>This way, I can limit the Value within MinValue and MaxValue and keeping everything with generic, thus avoiding overriding an abstract method in every derived class. </p>