Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem here ;
I've create a treeview in c# like this code ,but
How can I do this in wpf (xaml); ?:confused:

public void PopulateTreeView(  string dirValue, TreeNode parentNode )
        /*   dirValue = inputTextBox.Text
             parentNode = directoryTreeView.Nodes[0] ..
            directoryTreeView is a Treeview

         // array stores all subdirectories in the directory
        string[] directoryArray =  Directory.GetDirectories( dirValue);

           // subdirectories are present
           if ( directoryArray.Length != 0 )
              //  create new TreeNode,

              foreach ( string directory in directoryArray )
                  // substringDirectory is a string defined in generall
          substringDirectory = Path.GetFileNameWithoutExtension(directory );

                  // current directory
                 TreeNode myNode = new TreeNode( substringDirectory );

                 // add current directory node to parent node
                 parentNode.Nodes.Add( myNode );

                 // recursive populate
                 PopulateTreeView( directory, myNode );

        catch ( UnauthorizedAccessException )
           parentNode.Nodes.Add( "Access denied !" );

Edit:: Thanks for help, but I really didn't understand what do you guys mean ! can you post snippet code ?(cause I'm new to xaml). :rose:
Updated 28-Apr-10 4:05am

You can do it pretty much the same way. In fact, the code should pretty much be able to be dropped in exactly the way you already have it (with the appropriate variable name change for the treeview itself).
Share this answer
You can use TreeViewItem class in WPF or else write your output in a XML file and use the same file as the source of TreeView in XAML.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900