开发者

Validating a color in silverlight

I construct some xaml in a template class and I need a rectangle to h开发者_如何学运维ave a Fill property set to something coming from a textbox filled by the end user. Therefore I need to validate that the value is convertable to a Brush object before outputting the xaml. Otherwise, if the value is not a valid color (named or not) I get following : Error: Sys.InvalidOperationException: Invalid XAML for control 'sldivparent'. [] (line 6, col 412): Failed to create a 'System.Windows.Media.Brush' from the text 'NotValidColorValue'. Obviously, I don't want that and in case of invalid value I just want it to be White.

So how could I make sure 'NotValidColorValue' is a valid color value before proceeding?

Thanks

Edit for further clarification: I am using color picker, calling a YUI color picker from silverlight to be exact and its working fine. The thing is that a user can also type in a color (either #112233 or "Red") directly in the textbox and that needs to be valid or display white if invalid. I don't want to go into the whole manual check of color names, so ideally I'd like to attempt to create an object of some type (maybe some Brush derivative) using whatever user has typed in and if that doesn't succeed I'd know the color value was invalid and act accordingly.


Update
Did some more thinking about this problem. You can just bind the value :) This will both validate the color and set it. An invalid value will use the FallbackValue White.

<Rectangle Fill="{Binding ElementName=textBox,
                          Path=Text,
                          FallbackValue=White}"/>
<TextBox Name="textBox" Text="Green"/>

And if you need to have methods like IsColorValid and GetColorFromValue in code behind you can use Bindings there as well like this

private bool IsValidColor(string colorValue)
{
    if (colorValue == "White" || colorValue == "#FFF" || colorValue == "#FFFF" ||
        colorValue == "#FFFFFF" || colorValue == "#FFFFFFFF")
    {
        return true;
    }
    Brush fallbackBrush = new SolidColorBrush(Colors.White);
    Rectangle validationRectangle = new Rectangle();
    Binding validationBinding = new Binding();
    validationBinding.Source = colorValue;
    validationBinding.FallbackValue = fallbackBrush;
    validationRectangle.SetBinding(Rectangle.FillProperty, validationBinding);
    if (validationRectangle.Fill == fallbackBrush)
    {
        return false;
    }
    return true;
}
private Color GetColorFromValue(string colorValue)
{
    Rectangle validationRectangle = new Rectangle();
    Binding validationBinding = new Binding();
    validationBinding.Source = colorValue;
    validationBinding.FallbackValue = new SolidColorBrush(Colors.White);
    validationRectangle.SetBinding(Rectangle.FillProperty, validationBinding);
    return ((SolidColorBrush)validationRectangle.Fill).Color;
}


Not sure what you want but if you want to validate #11223344 values, use Regex validation using something like following pattern:

(^#[0-9A-Fa-f]{8}$|^#[0-9A-Fa-f]{6}$|^#[0-9A-Fa-f]{3}$)

Now, if you want a user to type down a name of color, try using something like Color ComboBox instead.

And finally, I would recommend using a ColorPicker above all. Its about providing a Rich UI after all.


You could try creating the Brush object with whatever the user enters. If it throws an exception, catch it and let the user know. Otherwise, it's a valid Color.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜