Question Changing the Background color of some ListBox Items

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
The ListBox is defined as follows:
C#:
 <ListBox x:Name="EditableStructs" Grid.Row="5" Grid.Column="1" Margin="930,62,0,40"  SelectionMode="Single"  Grid.ColumnSpan="2" Background="PowderBlue" Height="500"
                           ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True"  ItemsSource="{Binding AutoNames,Mode=TwoWay}" HorizontalAlignment="Left" Width="220"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <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>
            </ListBox.ItemTemplate>
        </ListBox>
The number of items as well as which items should have a different color is known at runtime when the strings to be displayed are being generated.

C#:
 foreach (string str in EditedList)
            {
                AutoNames.Add(new EditableStructures { StrName = str,  IsAccepted = false, NamInd = j});
                j++;
            }

EditableStructs.ItemsSource = (System.Collections.IEnumerable)AutoNames;

Clearly indexing the ListBox makes sense after the ListBox has been generated.
My problem is to select programmatically the ListBox items whose color is to be changed:
I tried the following:

C#:
 for(int i =0; i < EditableStructs.Items.Count; i++)
            {
                if (IsGuessed[i])
                    EditableStructs.TextBox[i].Background = "Red";
            }
But it does not work even if I replace TextBox with Items

I would appreciate any kind of help. Thank you in advance
 
This is why I said in your other thread:
If you continue to write code behind ala WinForms, then you'll have to figure out which UI element you need to update.
The brute force approach would be to walk down the WPF's UI hierarchy at runtime to find those UI elements. You'll likely have to use the VisualTreeHelper class to do this.

A slightly less brutish way is to add the UI elements yourself in a loop so that you at least have references to them at the point in time when you construct them. That means you'll have to throw away that ItemTemplate that you currently have in the XAML.
 
This is why I said in your other thread:

The brute force approach would be to walk down the WPF's UI hierarchy at runtime to find those UI elements. You'll likely have to use the VisualTreeHelper class to do this.

A slightly less brutish way is to add the UI elements yourself in a loop so that you at least have references to them at the point in time when you construct them. That means you'll have to throw away that ItemTemplate that you currently have in the XAML.
Do you mean to dynamically build the ListBox with C# code?
I am afraid I do not have the experience and the knowledge it takes.
I have thought of a workaround. After all, I have been asked to do something to help users focus on reviewing only the structure names that have been guessed through the Dice coefficient.
I will add the 4th field to each row of the ListBox containing either an empty string of a red "*" beside the names that have been guessed.
 
Back
Top Bottom