Click here to Skip to main content
15,504,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textblock, the text's foreground's color should change by the value. However it is not working well.

<TextBlock Text="{Binding Path=Foo}" 
           Foreground="{Binding Path=Foo, Converter={StaticResource myConverter}" />


The converter part likes
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var temp = (int)value;
        if (temp <100 && temp>50)
        {
           return "#ff23d3";
        }
       if(temp>200)
           return "da2367";
       return new SolidBrush(Colors.Green);
    }

But although the values are different, it only return one color.

What I have tried:

I set the break point in the code. It did reach the right place.
Maybe type conversion is wrong?
Posted
Updated 16-Nov-17 22:52pm
Comments
Kenneth Haugland 16-Nov-17 20:38pm    
What is it bonded to? Did you implement INotifyChanged on that property?

1 solution

First things first, the Foreground[^] Dependency Property[^] of the of any WPF Control[^] is expecting a Brush[^].

Armed with this knowledge, we can now look at your Converter:
C#
public object Convert(object value,
                      Type targetType,
                      object parameter,
                      System.Globalization.CultureInfo culture)
{
    var temp = (int)value;
    if (temp <100 && temp>50)
    {
       return "#ff23d3";
    }
    if(temp>200)
       return "da2367";
    return new SolidBrush(Colors.Green);
}


1. "#ff23d3" = string hex color value (not color object), not a Brush of any kind.

2. "da2367" is a string. Not the same as the first, but still not a Brush.

3. new SolidBrush(Colors.Green) - This is a Solid Color Brush. This will work.

So, to fix, you need to convert a color value to a Color class[^] that can be used with a Brush:
C#
using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFFF23D3");

Now that you have a Color object you can apply it to a SolidColorBrush[^]:
C#
return new SolidBrush(color);

So, your fixed Converter will look like:
C#
public object Convert(object value,
                     Type targetType,
                     object parameter,
                     System.Globalization.CultureInfo culture)
{
    var temp = (int)value;

    if (temp <100 && temp>50)
       return new SolidColorBrush(
           (Color)ColorConverter
               .ConvertFromString("#FFFF23D3"));

    if(temp>200)
       return new SolidColorBrush(
           (Color)ColorConverter
               .ConvertFromString("#FFDA2367"));

       return new SolidColorBrush(Colors.Green);
}
 
Share this answer
 
v3
Comments
Member 12658724 17-Nov-17 8:51am    
Something wrong on the code new SolidBrush(
(Color)ColorConverter
.ConvertFromString("#FFFF23D3"));

'Color' is a type, which is not valid in the given context.
Graeme_Grant 17-Nov-17 8:54am    
I wrote the code off the top of my head (untested). Once tic...

edit: Typo ... copied from your code ... change SolidBrush to SolidColorBrush ... sorry. SolidBrush is GDI+ and not System.Windows.Media (WPF)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900