简体   繁体   中英

Is it possible to return more than one value from an ivalueconverter in wpf?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM