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:"
---
I modified the wpf code to:
This wpf code does not change the selected item background color.
When I change the Value to False:
it does change all of the not highlighted items background to blue.
When I set the Foregound to red, it works.
Please advise.
Tuhin PaulPosted Mar 14, 2024, 7:31 PM
Valueof theSetterproperty to any desired color for the selected item background.Style.Triggerssection to style non-selected items or items in other states (e.g., hovering over an item).Tuhin PaulPosted Mar 14, 2024, 7:31 PM
The issue with your WPF code is that you might have accidentally set the
Valueproperty of theTriggertoFalseinstead of theConditionproperty.In your previous attempt, setting
Value="False"would have created a trigger that only fires when an item is not selected. Since there wasn't another trigger for non-selected items, they remained with the default background color. Similarly, setting the foreground to red likely worked because the default foreground color for ListBox items is usually black which gets overridden by the red color.