VisualElement dataSource is null even if added to parent

I have added a data source to one of my template instance created in my UI builder.

However I have found the the dataSource property of the UI element is null no matter what. As per another forum post dataSource of child items data source should be inherited from parent until overridden, and should not be null.

namespace Cirrus.UI
{
    [UxmlElement]
    public partial class QuickMenuVisualElement : BindableElement
    {
        private QuickMenuDataSource _dataSource;
        private VisualElement _entriesLayoutElement;
        [UxmlAttribute]
        [CreateProperty]
        public VisualTreeAsset _entryTemplate;
        private Dictionary<IAbilityInstance, VisualElement> _entries = new Dictionary<IAbilityInstance, VisualElement>();
        public QuickMenuVisualElement()
        {
            _AsyncInit();
        }
        private void _OnAbilityAdded(IAbilityInstance ab)
        {
            if (!_entries.ContainsKey(ab))
            {
                _entries.Add(ab, _entryTemplate.Acquire(_entriesLayoutElement));
            }
        }
        private void _OnAbilityRemoved(IAbilityInstance ab)
        {
            if (_entries.TryGetValue(ab, out VisualElement elem))
            {
                elem.Release();
                _entries.Remove(ab);
            }
        }
        async void _AsyncInit()
        {
            await UniTask.WaitForEndOfFrame();
           // This is null no matter what.
            if (dataSource != null)
            {
                _dataSource = (QuickMenuDataSource)dataSource;
                _dataSource.OnAbilityAddedEvent += _OnAbilityAdded;
                _dataSource.OnAbilityRemovedEvent += _OnAbilityRemoved;
            }
            _entriesLayoutElement = this.Query("Layout_Entries");
        }
    }
}

Hi @FoodFish_1 , the resolved dataSource is inherited when the binding update is processed, but we won’t set the dataSource of the all children in code.

If you need to get the resolved data source for that element at that point in time, you can use the following API:
element.GetHierarchicalDataSourceContext(). This will return the nearest data source it can find.

Hope this helps!

1 Like

Thank you. This isn’t a bug sorry. Unfortunately cannot update the title.

No worries!
I’ll see if we can update it to “Resoved”.