There are several examples of how to populate a tree view from a collection of file paths such as this or this other example. I cannot seem to find such example for WPF. I know I can integrate windows forms and use a different control in order to make it work but it will be nice if I could do the same thing with a wpf treeview control. The tree view that I want to construct consists of about 50,000 files therefore I think it will be better if it is bind it to something. But first before binding it, I think it will be helpful to construct one based on a List of strings (strings contains the paths of files).
Share
I was intrigued by the question and threw this together. As a first pass I think I’m pretty close to what you’re looking for. Talking about 50,000 items though makes me think that lazy loading may be appropriate. Anyway, here is the simple version based on an article by Josh Smith. I put all of the code here, but the magic really takes place with the data templates.
Given a few classes to represent the objects we’re working with…
and…
and…
I created a recursive method to load up some directories/files…
From there it’s just a matter of getting the data…
And displaying it…
All of the magic really happens with the data templates. I guess the key to the whole thing is using the HierarchicalDataTemplate for any items with hierarchy (i.e. directories).
NOTE 1: I haven’t extensively tested this. It hasn’t been profiled for performance. I would welcome any feedback though since this is a problem I tried to solve long ago and gave up on. Thanks!
NOTE 2: You’ll need to set the hard-coded path to something that makes sense on your system.
Here is a screenshot showing directories and files at different levels…