ColorCanvas hexadecimal string coercion

Jan 17, 2012 at 10:55 AM

In the current ColorCanvas implementation: if a hexadecimal string is invalid it doesn't revert to the previous valid value.

I suggest to coerce a hexadecimal string value, e.g.:

public static readonly DependencyProperty HexadecimalStringProperty = DependencyProperty.Register("HexadecimalString", typeof(string), typeof(ColorCanvas), new UIPropertyMetadata("#FFFFFFFF", OnHexadecimalStringChanged, OnCoerceHexadecimalStringProperty));

private static object OnCoerceHexadecimalStringProperty(DependencyObject d, object basevalue)
{
    var colorCanvas = (ColorCanvas)d;
    if (colorCanvas == null)
    {
        return basevalue;
    }

    return colorCanvas.OnCoerceHexadecimalString(basevalue);
}

private object OnCoerceHexadecimalString(object newValue)
{
    var value = newValue as string;
    try
    {
        ColorConverter.ConvertFromString(value);
    }
    catch
    {
        return HexadecimalString;
    }

    return value;
}

What do you think about it?

Jan 18, 2012 at 2:18 PM

I will check it out.  One thing to realize is that coerce doesn't update the source only the target.  SO if the expectationis to revet the source value thyen this needs to be done in the PropertyChangedCallback.

Feb 16, 2012 at 11:16 AM
brianlagunas wrote:

I will check it out.  One thing to realize is that coerce doesn't update the source only the target.  SO if the expectationis to revet the source value thyen this needs to be done in the PropertyChangedCallback.

Hi, Brian. What do you think about reversion of invalid hexadecimal string to the previous valid value?

Apr 12, 2012 at 1:57 PM

Is it possible to change the behaviour of ColorCanvas according to this discussion?

Editor
Nov 12, 2012 at 8:24 PM

This issue is fixed in v1.8.0