Mikael Gyth
Topic Author
Posts: 33
Joined: 15 Aug 2013, 12:53

[Unity] TreeView ItemsSource

15 Oct 2014, 14:14

Hello.
I read in some post that constructing a TreeViews content from CodeBehind was not supported yet. Is this correct?
If not, how can I set a TreeVeiws itemsource from codebehind in Unity?

I was able to create a collection and set it as the TreeViews source, but I was not able to create collections of collections to get nested elements in the TreeView.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: [Unity] TreeView ItemsSource

16 Oct 2014, 19:21

Hi,

To be able to correctly use TreeView.ItemsSource you would need some HierarchicalDataTemplates (here is some example: http://blogs.msdn.com/b/mikehillberg/ar ... -step.aspx).

Unfortunately we don't have support for HierarchicalDataTemplates yet. And as I explain here, the only option available now is to populate the TreeView with TreeViewItems manually created.

Anyway, we expect to have this implemented for the following release ;)
 
Genom
Posts: 47
Joined: 04 Dec 2014, 14:47

Re: [Unity] TreeView ItemsSource

01 Feb 2015, 22:28

Hi, regarding the TreeView manual implementation, I've been playing with and I found that it takes incredible amount of time to be loaded (5 or more seconds), am I in the correct way to do it? here you are my sample code:
public partial class CartaView : MonoBehaviour, IXamlLocalizable
    {
        private NoesisGUIPanel GuiPanel;
        private Grid DivContent;

        public string Xaml { get { return "Assets/Views/Inside/Carta/CartaView.xaml"; } }

        public CartaView()
        {
               
        }

        public void Start()
        {
            var tvCarta = UiHelper.FindChild<TreeView>("TvCarta");

            var count = 100;
            var items = new ItemCollection();
            
            for (var i = 0; i < count; i++)
            {
                var item = new TreeViewItem();
                var header = new TextBlock(i.ToString());
                header.SetForeground(new SolidColorBrush(Color.White));
                header.SetFontSize(40);
                var col = new ItemCollection();
                col.Add("a1");
                col.Add("a2");
                col.Add("a3");
                item.SetHeader(header);
                item.SetItemsSource(col);
                items.Add(item);
            }
            
            tvCarta.SetItemsSource(items);
        }
    }
 
Update: I found that the creation of the subitems (var col = new ItemCollection();) is what is consuming the extra time. I can declare them out of the loop, but I wonder what will happen when we have heavy data to load..

I'll try to find a way to load them async, one by one, a on the fly (when the initial state is collapsed) which would improve the user experience in case the time consuming cannot be avoided.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: [Unity] TreeView ItemsSource

03 Feb 2015, 18:15

Hi,

There are some time consuming tasks when you load and first show a UI tree in the screen:

- First all the tree must be Measured, that is, calculate the minimum space required by each element in the Visual tree. This step requires that all the controls expand its corresponding visual template, so the Visual tree is complete for measuring. This implies lots of allocations and calculations.
- Second the tree must be Arranged, that is, elements are given the final size and position inside their containers.
- Then we have to transfer all that information to the render thread.

You can check in the Unity profiler that the first frame after you attach your loaded xaml to the main window, NoesisGUIPanel.LateUpdate takes a lot of time (1.63 seconds in my case with your TreeView sample).

One change I will do to your code is instead of using the ItemsSource, as long as you are creating the TreeViewItems yourself, you can directly use the Items property:
var tvItems = tv.GetItems();
for (var i = 0; i < count; i++)
{
    var item = new TreeViewItem();

    var header = new TextBlock(i.ToString());
    header.SetForeground(new SolidColorBrush(Color.White));
    header.SetFontSize(40);
    item.SetHeader(header);

    ItemCollection items = item.GetItems();
    items.Add("a1");
    items.Add("a2");
    items.Add("a3");

    tvItems.Add(item);
}
I suggest, if possible, to fill the TreeView progressively as needed.

Who is online

Users browsing this forum: Ahrefs [Bot] and 20 guests