0

I have a wpf listbox that implements a DataTemplate that contains a TextBlock.

    <local:BooleanToFontColorConverter x:Key="boolToFontColor" />
    <DataTemplate x:Key="ListBox_DataTemplateSpeakStatus">
            <Label Width="Auto">
                    <TextBlock Foreground="{Binding Path=myProperty, Converter={StaticResource boolToFontColor}}" />
            </Label>
    </DataTemplate>

My task at hand is upon the changing of "myProperty", I want the color of the font to be different. My converter looks like this:

public class BooleanToFontColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                  CultureInfo culture)
    {
        if (value is Boolean)
        {
            return ((bool)value) ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black);
        }

        return new SolidColorBrush(Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This works. The font color (foreground) will turn red upon the changing of the bound property.

My question is this: I would like my font to change to being red, AND bold, AND italics. I know that this is possible via using textblock inlines, but is it possible to do all three of these things using my converter?

Thank you to everyone with thoughts and insight that respond.

H.B.
  • 166,899
  • 29
  • 327
  • 400
mherr
  • 348
  • 1
  • 7
  • 25

1 Answers1

3

Do not use converters for this, use a DataTrigger and add three respective Setters for the properties.

(You could return more than one object, but it would be pointless as all of those properties only take one object. An alernative would be using the Binding.ConverterParameter on which you then can switch in the converter to return the right value for the right property, you will still need three bindings, with a different parameter each, it's very ugly)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thank you for your quick comment. I will investigate how to use a DataTrigger! – mherr Jun 29 '12 at 14:22
  • Thanks Marc, I'm aware that that is how this site works. I took some time to investigate DataTriggers to see if that indeed would solve my problem. I've decided that I am not going to use DataTriggers, but they would work just fine if I chose to. – mherr Jun 29 '12 at 15:26