locked
C#.Net - Treeview.Nodes doesn't exist? RRS feed

  • Question

  • Hey there. Sorry if this isn't the right forum, to begin with. I'm new here. ^^. And new to C#/WPF development, so bear with me while I attempt to explain my problem.

    I'm making a simple little music browser, just to get to grips with the directory/file handling etc in C#, and a little I/O. So, I can grab the directory paths easily enough, but I'm having problems displaying them. I want to display them in a treeview, so you can like expand the artist name to get a list of albums, then expand the albums to get the songs etc. Anyway, I looked up the msdn page for treeview, and found I wanted to use Windows.Forms and Treeview.Node. So, I did the following:

    treeView1.Nodes.Add("Test");

    However, when I try to build it, I get the following error:

    'System.Windows.Controls.TreeView' does not contain a definition for 'Nodes' and no extension method 'Nodes' accepting a first argument of type 'System.Windows.Controls.TreeView' could be found (are you missing a using directive or an assembly reference?)



    So, what's going on? As far as I knew, TreeView.Nodes is located at System.Windows.Controls...

    =/

    Help please!

    -Edibles
    • Edited by ediblespread Thursday, May 14, 2009 10:00 PM
    • Moved by nobugz Friday, May 15, 2009 12:03 AM not a bcl q (From:.NET Base Class Library)
    Thursday, May 14, 2009 9:44 PM

Answers

  • There are both a Windows Forms TreeView and a WPF TreeView.  Since you are using WPF, you will need to avoid the members of the System.Windows.Forms namespace.

    The WPF TreeView has an Items property to which you generally add instances of TreeViewItem.
    • Marked as answer by ediblespread Friday, May 15, 2009 12:08 AM
    Thursday, May 14, 2009 10:54 PM

All replies

  • There are both a Windows Forms TreeView and a WPF TreeView.  Since you are using WPF, you will need to avoid the members of the System.Windows.Forms namespace.

    The WPF TreeView has an Items property to which you generally add instances of TreeViewItem.
    • Marked as answer by ediblespread Friday, May 15, 2009 12:08 AM
    Thursday, May 14, 2009 10:54 PM
  • Oh, thanks. Got it now. One further question, if you don't mind - how do you then sub-populate the tree? Say I've added the two entries "foo" and "bar", and now want to add "sub-foo" as a sub-category of foo (so that I can expand foo to see sub-foo), how do I go about doing this? Ironically, I know the Window Forms way, but I don't know the WPF XD.

    Thanks very much for the answer!

    -Edibles
    Thursday, May 14, 2009 11:16 PM
  • It is actually quite similar to the Windows Forms way.  Once again, it is Items, not Nodes.

                TreeViewItem tviI = new TreeViewItem() { Header = "I" };
                TreeViewItem tviIA = new TreeViewItem() { Header = "I.A" };
                TreeViewItem tviIA1 = new TreeViewItem() { Header = "I.A.1" };
                TreeViewItem tviIA2 = new TreeViewItem() { Header = "I.A.2" };
                TreeViewItem tviIB = new TreeViewItem() { Header = "I.B" };
    
                tviI.Items.Add(tviIA);
                tviIA.Items.Add(tviIA1);
                tviIA.Items.Add(tviIA2);
                tviI.Items.Add(tviIB);
    
                treeView1.Items.Add(tviI);
    Thursday, May 14, 2009 11:32 PM
  • Thank you! Much appreciated!

    -Edibles
    Friday, May 15, 2009 12:08 AM