1

from my Forms Windows App I'm used to build a TreeView like this

treeView1.Items.Add("Result");
treeView1.Nodes[0].Items.Add("Child 1");
treeView1.Nodes[0].Items.Add("Child 2");
treeView1.Nodes[0].Nodes[1].Items.Add("Grandchild");
treeView1.Nodes[0].Nodes[1].Nodes[0].Items.Add("Great Grandchild");

How am I supposed to do this in WPF? I'm confused with all the samples in the web!

Thanks!

Stef
  • 593
  • 3
  • 10
  • 23
  • possible duplicate of [populate treeview from list of file paths in wpf](http://stackoverflow.com/questions/6415037/populate-treeview-from-list-of-file-paths-in-wpf) – outis Jul 19 '12 at 00:39

1 Answers1

0

http://www.c-sharpcorner.com/uploadfile/mahesh/treeview-in-wpf/

SkonJeet
  • 4,827
  • 4
  • 23
  • 32
  • 2
    This way will work for simple applications, but if you want something more complex you really should do it the MVVM way. You need to create TreeViewItemViewModels, which implement INotifyPropertyChanged. These items will have properties of the models you are binding to them, (obviously with OnPropertyChanged events allowing for easy binding) as well as properties that help handle treeviewitem behavior (like IsEnabled, IsExpanded, Child and Parent properties for easy navigation etc). These properties will be bound to the TreeviewItem's properties so you can manipulate them in code. – Jason Ridge Apr 03 '12 at 15:23
  • MVVM is definitely the preferred option, agreed. The only reason I suggested such a simple tutorial is because there was no mention of underlying data to be bound to the control or any other context provided. +1 though, you're right. – SkonJeet Apr 03 '12 at 15:25
  • I just added the comment so he (and others) could read it as a cautionary tale. Your link is useful, but people should know it's limitations. That's why I didn't add it as an answer, because he probably is looking for something like that – Jason Ridge Apr 03 '12 at 15:27
  • I found this, which is perfect to start from: http://stackoverflow.com/questions/6415037/populate-treeview-from-list-of-file-paths-in-wpf – Stef Apr 30 '12 at 07:27