Clyde Eisenbeis

Clyde Eisenbeis

  • 1.7k
  • 85
  • 14.4k

How do change the Background color on one ListBox item? - Continued #3

Mar 14 2024 4:27 PM

C# wpf --- This is a "continuation" of c-sharpcorner.com/forums/how-do-change-the-background-color-on-one-listbox-item-continued-sharp2.

When I highlight one ListBox item,

gLBxEpidemicSequence.SelectedIndex = iIndex;

C#

the background is Gray.  How do I change the background color?

Note than when I set the Foreground to

 

 

---

The response, by Naimish Makwana, "In WPF, the ListBox control does not have a SelectionBackground property. Instead, you can change the background color of a selected item by modifying the ListBox’s item container style. Here’s an example:"

<ListBox x:Name="gLBxEpidemicSequence">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

---

I modified the wpf code to:

<ListBox x:Name="gLBxEpidemicSequence" >
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox> 

This wpf code does not change the selected item background color.

When I change the Value to False:

<Trigger Property="IsSelected" Value="False">

it does change all of the not highlighted items background to blue.

When I set the Foregound to red, it works.

Please advise.

 

 


Answers (2)