You could try to refer to the code below. You can also refer to here.
MainWindow.xmal:
<StackPanel>
<ListBox x:Name="EditableStructs" SelectionMode="Single" Width="400" Background="PowderBlue" Height="300"
ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True"
ItemsSource="{Binding AutoNames,Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Name="TextInd" Text="{Binding NamInd, Mode=OneWay}" Grid.Column="0" Padding="1,5" HorizontalAlignment="Stretch"/>
<CheckBox IsChecked="{Binding IsAccepted, Mode=TwoWay}" Grid.Column="1" Padding="5,5" VerticalAlignment="Center" HorizontalAlignment="Center" />
<TextBox Text="{Binding StrName, Mode=TwoWay}" Background="{Binding MyBackground}" Grid.Column="2" HorizontalAlignment="Stretch"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path= SpecialFeatures}">
<DataTrigger.Value>
<local:SpecialFeatures>Color</local:SpecialFeatures>
</DataTrigger.Value>
<DataTrigger.Setters>
<Setter Property="Background" Value="DodgerBlue" TargetName="grid" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding Path= SpecialFeatures}">
<DataTrigger.Value>
<local:SpecialFeatures>Highlight</local:SpecialFeatures>
</DataTrigger.Value>
<DataTrigger.Setters>
<Setter Property="Background" Value="Orange" TargetName="grid" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding Path= SpecialFeatures}">
<DataTrigger.Value>
<local:SpecialFeatures>None</local:SpecialFeatures>
</DataTrigger.Value>
<DataTrigger.Setters>
<Setter Property="Background" Value="white" TargetName="grid" />
</DataTrigger.Setters>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
MainWindow.xaml.cs:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace ListBoxBackgroundDemo
{
public partial class MainWindow : Window
{
public ObservableCollection<EditableStructures> AutoNames{ get;set;}
public List<string> EditedList=new List<string>(){ "name1", "name2", "name3", "name4", "name5", "name6" };
int j =0;
public MainWindow()
{
InitializeComponent();
AutoNames = new ObservableCollection<EditableStructures>();
DataContext =this;
foreach (string str in EditedList)
{
if (j % 3 == 0)
{
AutoNames.Add(new EditableStructures { StrName = str, IsAccepted = false, NamInd = j, SpecialFeatures = SpecialFeatures.Highlight });
}
else if(j%3==1)
{
AutoNames.Add(new EditableStructures { StrName = str, IsAccepted = false, NamInd = j, SpecialFeatures = SpecialFeatures.Color });
}
else
{
AutoNames.Add(new EditableStructures { StrName = str, IsAccepted = false, NamInd = j, SpecialFeatures = SpecialFeatures.None });
}
j++;
}
EditableStructs.ItemsSource = (System.Collections.IEnumerable)AutoNames;
}
}
public enum SpecialFeatures
{
None,
Color,
Highlight
}
public class EditableStructures : INotifyPropertyChanged
{
public string StrName { get;set;}
public bool IsAccepted { get;set;}
public int NamInd { get;set;}
private SpecialFeatures _specialFeatures;
public SpecialFeatures SpecialFeatures
{
get { return _specialFeatures; }
set
{
_specialFeatures = value;
OnPropertyChanged("SpecialFeatures");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html