Styles not applied to elements added from code

Let’s assume we have a simple test stylesheet:

The stylesheet is attached to the root of the visual tree. So far it works the way it should, all texts are red.

But if I dynamically add an element to the graph like this:

var template = Resources.Load<VisualTreeAsset>("MyTemplate");
var instance = template.CloneTree().ElementAt(0);
container.Add(instance);

The newly added element is not affected by the stylesheet of its parent VisualElement. It appears that I have to manually attach the stylesheet every time I use CloneTree():

instance.styleSheets.Add(styleSheet);

Am I doing anything wrong or is it a bug?

Hi oobartez,

Styles should applies to elements added from code.
Can you post the content of the “MyTemplate” UXML?

This is just a quick update that I tried to create a minimal repro but without success. The problem still occurs in our large project (even with that minimal example) but I am unable to reproduce it in a small side project. I will update you if we find out something more specific.