I found this post asking how to set the style of a ListView element , and it was supremely helpful, but I’ve got an issue where setting the style in the MakeItem function doesn’t appear to be overriding the style set by the item itself.
I’ve got a testItem where some text is displayed, but to keep things legible I put the text on a white background. The white background is set with a style sheet for the test item.
I’ve tried overriding the testItem style sheet two ways: the first was to add a list view subsection to the actual testItem style sheet. I thought this would work as described in the post I linked, but that doesn’t work. My testItem style “test-item-style” has
.test-item-style {
background-color: rgba(255, 255, 255, 255);
}
.test-item-style.unity-list-view__item {
background-color: rgba(255, 0, 0, 255);
}
but that doesn’t set the testItem style when the testItem becomes a “child” of the ListView.
I then tried adding a “listview-item” style sheet when the item is made
.listview-item.unity-list-view__item {
background-color: rgba(255, 0, 0, 255);
}
private VisualElement MakeItem()
{
var testItem = testItemTemplate.CloneTree();
testItem.AddToClassList("listview-item");
return testItem;
}
If I have the ListView’s itemHeight set too large then I can see that the background behind my testItem is doing what I want, but the “listview-item” style sheet isn’t overriding the actual testItem’s style sheet.
The only way I can seem to get the background set correctly for the items in the ListView is if I remove the style sheet from the TestItem. Then there’s nothing setting the background color, so the style set in the MakeItem function “wins” and the background is as-intended.
This is all great, but I want to use the TestItem in other places, too, not just the ListView, and I need the background white (or some other readable color) where it’s used in those other places. I don’t want to have to go around and set the background everywhere I use the TestItem, I’d like to just override the TestItem’s background style when I need to, but again I’m not sure how I get at it to override it.
:EDIT:
I was reading a bit more about this while investigating with the UIDebugger. I found that the instantiated template is going into a “TemplateContainer” and the style I’m assigning in the MakeItem function is being applied to the TemplateContainer. @uDamian explained here that they are instantiated as TemplateContainers, “because the UXML can have multiple elements at its root.”
This would be fine for me, except I can’t assign stylesheets at the UXML/TemplateContainer level. This leaves me with no clean way to override the style sheet except by instantiating the VisualTreeAsset, then knowing what inside there needs to get overwritten. I guess it’s doable but doesn’t feel right.
If there’s a better way I’d love to hear it.
:EDIT 2:
Wrote that last night, tried it today, doesn’t work quite exactly as I thought. It’s not enough to know what inside the TemplateContainer needs to get overwritten - I need to actually go in and remove the style sheet I want to “override.” Here’s how I got the functionality I want.
private VisualElement MakeItem()
{
var testItemTemplateContainer = testItemTemplate.CloneTree();
testItemTemplateContainer .AddToClassList("listview-item");
var testItem = testItemTemplateContainer.Q<TestItemViewModel>("test-item");
testItem.RemoveFromClassList("test-item");
return testItemTemplateContainer;
}
I tried it a different way first, by specifying a “child” style: “.listview-item.test-item” and that was able to override the “test-item” style, but the “.listview-item.unity-list-view__item” style was never applied. When I set the “.listview-item.test-item” background to transparent, I got the “default” appearance of the ListView hover/selected states instead of the “.listview-item.unity-list-view__item:hover” and “.listview-item.unity-list-view__item:selected” backgrounds I had set. If I went into the element and removed its “test-item” style, and then also removed the “.listview-item.test-item” style, THEN I got my unity-list-view__item states to display correctly.
This is my first foray into “front-end” development, and it’s been kicking my butt. I’ve never used CSS or anything like this before, so I don’t know what the expected behaviors are for stuff like this. I can’t tell if this is a bug or not. By the name “cascading style sheets,” I would expect the last-applied thing to take precedent, so maybe this is on purpose? I.e., we draw the VisaulElement frame, then the ListView container, then the ListView item, and then my TestItem is (inside of?) the ListView item.
Having to reach into the TestItem, know in advance what its style sheet is, and then remove it feels dirty to me, but again I’m a novice on this end of things so maybe that’s just “how it’s done.” I would love some feedback on this, whatever the case.

