How do I change the selected style of a ListView element?

As the title states, how do I change it. Right now I am using a label with a white background and an outline for the items. When I select an item the text turns white and makes it impossible to see the text of the selected item. If I leave it a plain label with no background, it turns the text white and changes the background to blue.

You could change the style of selected items in ListView.onSelectionChange event, but the easier and perhaps better approach is style selector.

  1. Create uss file with selector that targets elements with your custom class and “.unity-list-view__item–selected”
  2. add your desired styling to it.
.myList-item.unity-list-view__item--selected {
    background-color: rgb(255, 0, 0);
}
  1. In your list view setup, add your custom class to list view item
Label MakeItem()
{
    var label = new Label("Hello");
    label.AddToClassList("myList-item");
    label.styleSheets.Add(styleSheet);
    return label;
}
  1. Done, should work
1 Like

Thank you so much. Don’t know much about uss/css and the new UI. I’ve been using the UI Builder to make an item database system and I’m really liking it so far. It took me very little time to get things up an running compared to the old system. The style thing isn’t as intuitive, but it is quite flexible.