Question

I have a text box for user to enter 6 character hex color value, a validator & a converter attached to it. Up to here, everything works fine. But I want to bind a textbox's Background color to the color specified by the text box (ElementNames Background & Foreground), and it does not seem to work.

When I debug/step through the code, the value seems like is always ""

XAML

<TextBox x:Name="Background" Canvas.Left="328" Canvas.Top="33" Height="23" Width="60">
    <TextBox.Text>
        <Binding Path="Background">
            <Binding.ValidationRules>
                <validators:ColorValidator Property="Background" />
            </Binding.ValidationRules>
            <Binding.Converter>
                <converters:ColorConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>
<TextBlock Canvas.Left="403" Canvas.Top="12" Text="Foreground" />
<TextBox x:Name="Foreground" Canvas.Left="403" Canvas.Top="33" Height="23" Width="60">
    <TextBox.Text>
        <Binding Path="Foreground">
            <Binding.ValidationRules>
                <validators:ColorValidator Property="Foreground" />
            </Binding.ValidationRules>
            <Binding.Converter>
                <converters:ColorConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>

<!-- in this example I used the converter used in the TextBox & another converter that converts a string to color -->
<TextBox ... 
    Background="{Binding ElementName=Background, Path=Text, Converter={StaticResource colorConverter}}" 
    Foreground="{Binding ElementName=Foreground, Path=Text, Converter={StaticResource stringToColorConverter}}" />

Converters

class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            string entry = ((Color)value).ToString();
            return entry.Substring(3); 
        } catch (Exception) {
            return Binding.DoNothing;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string entry = (string)value;
        Validators.ColorValidator validator = new Validators.ColorValidator();
        if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid) {
            return Binding.DoNothing;
        }
        return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
    }
}

class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string entry = (string)value;
        Validators.ColorValidator validator = new Validators.ColorValidator();
        if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid)
        {
            return Binding.DoNothing;
        }
        return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Was it helpful?

Solution

Everyone saying that you need a brush and not a color is right.

Solution: Create another converter that returns a SolidColorBrush and you will be golden.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top