I have a WPF ListBox containing a list of objects from a specific class that I have. Something like that:
ObservableCollection<MyTable> tables = new ObservableCollection<MyTable>(); ... listTables.ItemsSource = tables;
And XAML:
<ListBox HorizontalAlignment="Left" Margin="8,10,0,0" Name="listTables" Width="153" ItemsSource="{Binding tables}" SelectionChanged="listTables_SelectionChanged" Height="501" VerticalAlignment="Top"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="1"> <TextBlock Grid.Column="1" Text="{Binding tableName}" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Everything works perfectly. Now I want to have a different background for each item in the ListBox depending on the specific class property. For example, suppose the MyTable class has a property called isOccupied. If this flag is set for a specific item, I want it to have a red background in the ListBox, if it is not, then I want to have it with a green background. If the property changes, then the background should change accordingly.
Any tips on how to achieve this? I am looking at some information about ItemContainerStyle at the moment, but I'm relatively new to this, so I'm not sure if I am going the right way.
c # wpf xaml listbox
mmvsbg
source share