I’d like the child visual elements I have in my list-items to change color when the item is selected to contrast the blue background.
This is how my list items look. Notice the label text and the image are not white to contrast the blue background:
This is how the list items look in the unity windows. Notice how both the text and icon of the Visual Element are changed to an off-white to contrast the blue background:
When I make my List-item just a single Label, the font color changes appropriately; I’m only running into this issue with the child elements. How can I make my visual elements in my list item container respect the highlighting contrast?
You can just create a class in your USS file that sets the highlight color. And when you want to highlight an element you can enable the class on the selected item and disable it for all non selected items.
Yep… Looking through the source code, I think the best way for me to do that is plug into the onselect callback API. It would be easier for me to override ListView and add a new RecycledItem class; however, I’m not sure how the official version of that class will change over time.
This would be even easier to implement if there were OnAddToClassList and OnRemoveFromClassList events that I could register callbacks for. Then I could just tell my child elements to add/remove those classes when their parent does.
There are a number of methods on the base VisualElement object that allows you to add and remove classes from the class list quite easily. These are:
AddToClassList(string className)
RemoveFromClassList(string className)
ToggleInClassList(string className)
ClearClassList() //Removes all classes from the element
ClassListContains(string className) //Returns true if the class exists on the element
For whatever reason, the documentation only has details for EnableInClassList (Which seems to pull double duty for Add and Remove, using the enable bool to state whether you actually want to add it) and ToggleInClassList, but the other methods do exist and work as you expect.