Color check in silverlight

I create some xaml in the template class, and I need a rectangle to have the Fill property set by something coming from a text field filled in by the end user. So I need to check that the value is converted to a Brush object before xaml output. Otherwise, if the value is not a valid color (with or without a name), I get the following: Error: Sys.InvalidOperationException: Invalid XAML for the 'sldivparent' control. [] (line 6, col 412): Failed to create "System.Windows.Media.Brush" from the text "NotValidColorValue". Obviously, I don't want this, and in the case of an invalid value, I just want it to be white.

So, how can I make sure that “NotValidColorValue” is a valid color value before continuing?

thank

Edit for further clarification: I use the color picker, invoking the silverlight YUI color picker shortcut to be precise, and its performance. The fact is that the user can also enter a color (either # 112233 or "red") directly into the text field, and it must be valid or display white if it is invalid. I don’t want to go to all the manual checking of color names, so ideally I would like to try to create an object of some type (possibly some Brush derivative) using any user, typing it, and if that fails, I would know the color value is invalid and acts accordingly.

+3
source share
3 answers

Update I
also thought about this problem. You can just bind the value :) This will check the color and set it. An invalid value will use 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 your code, 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;
}
+3
source

Not sure what you want, but if you want to check the values #11223344, use Regex validationusing something like the following pattern:

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

Now, if you want the user to type a color name, try using instead Color ComboBox.

, , ColorPicker. Rich UI .

+1

You can try to create an object Brushwith what the user enters. If it throws an exception, catch it and report it to the user. Otherwise, it is valid Color.

0
source

Source: https://habr.com/ru/post/1782295/


All Articles