Search code examples
wpflistviewlistviewitemivalueconverter

ListViewItem not Changing Foreground Color with a Bound IValueConverter


My trouble is that I want my WPF ListView to display ListViewItems in different colors, according to the items bound to them. Below is all the code to repro the problem. The app has Widgets that are either "in stock" or not. If they're in stock, they should have green text in the ListView, otherwise red. The coloring is handled by my BoolToColorConverter (transforming Widget.InStock to Colors.Green or Colors.Red). Breakpoints assure me Convert() is being hit. I'm probably doing something obviously wrong, but I'm at a loss since hard-coding the Foreground color works as expected. Thanks in advance.

MainWindow.xaml

<Window x:Class="ListViewItemColors.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ListViewItemColors" Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:WidgetsViewModel />
</Window.DataContext>
<Window.Resources>
    <local:BoolToColorConverter x:Key="BoolToColor" />
</Window.Resources>
<DockPanel >
    <ListView ItemsSource="{Binding Path=Widgets }">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="150" Header="Widget Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" Foreground="{Binding Path=InStock, Converter={StaticResource BoolToColor}}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="50" Header="Size" DisplayMemberBinding="{Binding Size}"></GridViewColumn>
                <GridViewColumn Width="75" Header="In Stock?" DisplayMemberBinding="{Binding InStock}"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

BoolToColorConverter.cs

public class BoolToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = (bool)value;
        return val ? Colors.Green : Colors.Red;
    }

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

WidgetsViewModel.cs

public class WidgetsViewModel
{
    public WidgetsViewModel()
    {
        Widgets = new Widgets
                      {
                          new Widget {InStock = true, Name = "Flipper", Size = 10},
                          new Widget {InStock = false, Name = "Gizmo", Size = 6},
                          new Widget {InStock = true, Name = "Gizmo", Size = 8},
                          new Widget {InStock = true, Name = "Whirlygig", Size = 15},
                          new Widget {InStock = false, Name = "Gutter", Size = 1},
                          new Widget {InStock = false, Name = "Gutter", Size = 2}
                      };
    }

    public Widgets Widgets { get; set; }
}

Widget.cs

public class Widget : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    private int _size;
    public int Size
    {
        get { return _size; }
        set
        {
            if (_size == value) return;
            _size = value;
            OnPropertyChanged("Size");
        }
    }

    private bool _inStock;
    public bool InStock
    {
        get { return _inStock; }
        set
        {
            if (_inStock == value) return;
            _inStock = value;
            OnPropertyChanged("InStock");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

Widgets.cs

    public class Widgets : ObservableCollection<Widget> { }

Solution

  • The TextBlock.Foreground property takes a Brush, not a Color. Change your converter so that it returns a SolidColorBrush, e.g.:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = (bool)value;
        return new SolidColorBrush(val ? Colors.Green : Colors.Red);
    }