I’m creating a VisualElement that instantiates all of it’s child VisualElements in the C# code (it doesn’t have a UXML file). I know I can set the content container in UXML, but how do I do that in C#? There is a “contentContainer” property on the VisualElement, but it is read only.
visualElement.contentContainer is getter only, not quite sure what you mean by setting content container
Override the contentContainer property to point to a child element.
Then use hierarchy.Add() to attach the content container (and any other elements) to the parent.
class MyElement : VisualElement
{
public new class UxmlFactory : UxmlFactory<MyElement> { }
public override VisualElement contentContainer => _container;
private VisualElement _container;
public MyElement()
{
_container = new VisualElement();
_container.name = "content-container";
hierarchy.Add(_container);
}
}
2 Likes