Change individual layout of ListViewItem programmatically

I have ListView

with 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 background ListViewItem

(or lines in the grid) or foreground to whatever color I want, for example

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 that pretty much explains what I want to achieve. Any help?

Thanks!

0


a source to share


1 answer


How about this approach:



  • Bind the foreground and background colors to the Person object. Let's call this property PersonStatus and set its type to a state enumeration just for this example.
  • Make sure these entities implement INotifyPropertyChanged and the setter for that property calls ProperyChanged . 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"));
            }
        }
    }
    
          

  • Create an IValueConverter that takes a status type and returns Brushes.Black, Brushes.Red, etc. Something like that:

    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();
        } 
    }
    
          

+2


a source







All Articles