- Mikael Gyth
- Posts: 33
- Joined:
[Unity] TreeView ItemsSource
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.
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.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: [Unity] TreeView ItemsSource
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
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
Re: [Unity] TreeView ItemsSource
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:
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.
Code: Select all
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);
}
}
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.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: [Unity] TreeView ItemsSource
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:
I suggest, if possible, to fill the TreeView progressively as needed.
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:
Code: Select all
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);
}
Who is online
Users browsing this forum: Ahrefs [Bot] and 20 guests