How to populate a TreeView?

Skip to first unread message

Vanessa Rodríguez

May 9, 2015, 11:18:58 AM5/9/15

Recently I got to know about Eto, I think you're doing a wonderful work with it.

So, to introduce myself to it, I started to create an application. I have almost everything finished minus a little problem I came across recently: I need to create a TreeView and populate it with the system's folder names, but I don't know how.

So far I managed to create the TreeView, but I can't seem to be able to populate it with the right data, or any data at all.
Based on the DataGridView example you have on gitHub, here's the code I have:

treeIzq = new TreeView();
var rootDirectoryInfo = new DirectoryInfo(path);
var directorios = new ObservableCollection<PathTreeNode>();
directorios.Add(CreateDirectoryNode(rootDirectoryInfo, 1));
treeIzq.DataStore = directorios;

As it is, it won't build and shows an error saying that it can't convert PathTreeNode to ITreeStore, that I should use a cast.
When I use the cast
treeIzq.DataStore = (ITreeStore)directorios;
it builds fine but throws an exception when it starts to run saying that it can't make the conversion to ITreeStore.

PathTreeNode is a custom class I implemented, it inherits from TreeItem and has two properties: a path to a file, and the filename.

I tried searching for any example of this, but I got nothing, and every idea I had failed, so right now I don't know what to make of it.
Could you help me? How can I populate the TreeView?

Thanks in advance,


May 9, 2015, 10:01:38 PM5/9/15
Hi Vanessa,

You should take a look at the TreeView example here.  It doesn't work the same as the GridView, as it doesn't use ObservableCollection.

You can use the TreeItemCollection as your DataStore instead of ObservableCollection.  It is not MVVM enabled like GridView is, so you have to call TreeView.RefreshItem when the item or its children change.

Hope this helps!

Vanessa Rodríguez

May 10, 2015, 1:36:04 PM5/10/15
Hi Curtis,

Thanks a lot for your answer.
I've read the example and changed the former ObservableCollection to TreeItemCollection as you said, it worked like a charm!


Apr 7, 2017, 6:08:26 PM4/7/17
to Eto.Forms
Hi Curtis,

sorry for replying to Vanessa answer instead of to your answer, but I don't understand how to do it (shame on me :(
(by the way great piece of sw this Eto.Forms ;)

I'm having a similar problem,
basically I create a TreeItemCollection and assign it to the DataStore of a TreeView, and then I populate it.
What happens is that the TreeView gets populated but no items are shown even if I call RefreshItem for the parent of item inserted (I also Expand the parent).
Only if I click on the TreeView I get it refreshed, but I'd like to have it refreshed automatically.
I tried with Focus() and SelectedItem in the hope to kind of emulate the click but with no luck.

Can you help?


Apr 9, 2017, 4:06:41 PM4/9/17
to Eto.Forms
Found the catch (it was my fault) but I'm posting it in case someone bumped into my same problem:
I was populating the TreeView from a different thread (not the UI one),
apparently under mono on Mac OS doing so it is not crashing the application,
anyway once I accessed the UI thread with the SynchronzationContext the tree got populated and refreshed without having to click on it.


Reply all
Reply to author
0 new messages