Reputation: 378
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.
Upvotes: 0
Views: 1370
Reputation: 184506
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)
Upvotes: 3