Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6991203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:27:57+00:00 2026-05-27T19:27:57+00:00

My trouble is that I want my WPF ListView to display ListViewItems in different

  • 0

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> { }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:27:57+00:00Added an answer on May 27, 2026 at 7:27 pm

    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);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few different tables that I want to display vertically down a
Im having trouble getting my JTable that im using to display either check boxes
I'm having trouble with the code below that refers to StoredPathName . As Indy
I'm having trouble with caching URLResponses to disk. I want that because the data
I am having trouble with a QT clustering implementation that I want to implement
I'm having some trouble with Parent Child NSManagedObjectContext. The issue is that I want
I have a WPF ListView inside StackPanel, with Height=Auto. It's great that it does
I have a WPF ListView inside StackPanel, with Height=Auto. It's great that it does
I'm having some trouble getting my infowindow to have the size that I want.
I'm having trouble with something that I understand should function out of the box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.