Question

I have a ListView with a GridView in it:

<ListView x:Name="listViewPersons" ItemsSource="{Binding Path=Persons, Mode=OneWay}" IsSynchronizedWithCurrentItem="True" 
                  Height="Auto" HorizontalAlignment="Stretch" Margin="4,4,4,4" Grid.Row="4" VerticalAlignment="Stretch" Width="Auto">
            <ListView.View>
                <GridView x:Name="gridViewPersons">
                    <GridViewColumn Header="Enabled">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <CheckBox HorizontalAlignment="Center" IsChecked="true" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Path=Name}" Width="142" />
                    <GridViewColumn Header="Age"  DisplayMemberBinding="{Binding Path=Age}" Width="142" />
                    <GridViewColumn Header="Gender"  DisplayMemberBinding="{Binding Path=Gender}" Width="142" />
                </GridView>
            </ListView.View>
        </ListView>

I want to be able to programmatically change each of the ListViewItems (or the rows in the grid) backgrounds or foregrounds to any color I want, for instance

listViewPersons.Items[0].Background = Brush.Red;
listViewPersons.Items[1].Background = Brush.Blue;
listViewPersons.Items[2].Background = Brush.Green

I know the previous lines of code don't work, but it pretty much explains what I want to achieve. Any help?

Thanks!

Was it helpful?

Solution

How about this approach:

  1. Bind ForegroundColor and Background color to a property in Persons. Let's call this property PersonStatus, and set its type to a status enum, just for this example.
  2. Ensure that this Persons implements INotifyPropertyChanged, and the setter for this property raises a ProperyChanged event. The property will look like this:

    public Status PersonStatus
    {
        get
        {
            return status;
        }
        set
        {
            if (value != status)
            {
                status= value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("PersonStatus"));
            }
        }
    }
    
  3. Create an IValueConverter that takes the type of Status and returns a Brushes.Black, Brushes.Red, etc. Something like this:

    public class StatusToColorConverter : IValueConverter 
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(Status))
                throw new InvalidOperationException("targetType must be Status");
    
            Status status = (Status)value;
    
            switch (status)
            {
                case Status.New:
                    return Brushes.Black;
                case Status.Professional:
                    return Brushes.Blue;
                case Status.Delete:
                    return Brushes.Red;
                default:
                    return Brushes.Black;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        } 
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top