Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Is WPF TreeView binding supposed to work like this (no collapse triangle)? [modified]

Is WPF TreeView binding supposed to work like this (no collapse triangle)? [modified]

Scheduled Pinned Locked Moved WPF
wpfhelpquestioncsharpwcf
16 Posts 2 Posters 31 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    artwallacex
    wrote on last edited by
    #1

    Hi, I hope someone can help me understand what is going on with WPF TreeView binding. My project has been having some weird issues with treeviews bound to a recursively created collection. The problem is that some nodes haven't had an expander/collapser triangle even though they still have subnodes (they can be doubleclicked to expand/collapse though). Here are two small pictures of the issue I'm having: http://www.crankedup.com/misc/wpf-treeview-notriangle.png[^] http://www.crankedup.com/misc/wpf-treeview-notriangle2.png[^] The second pic is the expanded version of the first one. The code that created this is: TreeNode tn1 = new TreeNode(@"C:\"); TreeNode tn2 = new TreeNode(@"C:\"); TreeNode tn3 = new TreeNode(@"C:\"); TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); TreeNodeList.Add(tn1); // Works fine The treenode class is the standard stuff: public class TreeNode { private string dir = ""; private List<treenode> children = new List<treenode>(); public TreeNode(string directory) { dir = directory; } public string Dir { get { return dir; } set { dir = value; } } public List<treenode> Children { get { return children; } set { children = value; } } } And the XAML binding was taken from examples I've seen on the web: <Window.Resources> <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Path=Children}"> <StackPanel Orientation="Horizontal"> <Image Source="Graphics\folder.png" /> <TextBlock Text="{Binding Path=Dir}" Margin="5,0" /> </StackPanel> </HierarchicalDataTemplate> </Window.Resources> <TreeView HorizontalAlignment="Left" Margin="8,9,0,38" Name="treeView1" Width="188" ItemsSource="{x:Static local:Window1.TreeNodeList}" /> What am I doing wrong here?

    L 1 Reply Last reply
    0
    • A artwallacex

      Hi, I hope someone can help me understand what is going on with WPF TreeView binding. My project has been having some weird issues with treeviews bound to a recursively created collection. The problem is that some nodes haven't had an expander/collapser triangle even though they still have subnodes (they can be doubleclicked to expand/collapse though). Here are two small pictures of the issue I'm having: http://www.crankedup.com/misc/wpf-treeview-notriangle.png[^] http://www.crankedup.com/misc/wpf-treeview-notriangle2.png[^] The second pic is the expanded version of the first one. The code that created this is: TreeNode tn1 = new TreeNode(@"C:\"); TreeNode tn2 = new TreeNode(@"C:\"); TreeNode tn3 = new TreeNode(@"C:\"); TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); TreeNodeList.Add(tn1); // Works fine The treenode class is the standard stuff: public class TreeNode { private string dir = ""; private List<treenode> children = new List<treenode>(); public TreeNode(string directory) { dir = directory; } public string Dir { get { return dir; } set { dir = value; } } public List<treenode> Children { get { return children; } set { children = value; } } } And the XAML binding was taken from examples I've seen on the web: <Window.Resources> <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Path=Children}"> <StackPanel Orientation="Horizontal"> <Image Source="Graphics\folder.png" /> <TextBlock Text="{Binding Path=Dir}" Margin="5,0" /> </StackPanel> </HierarchicalDataTemplate> </Window.Resources> <TreeView HorizontalAlignment="Left" Margin="8,9,0,38" Name="treeView1" Width="188" ItemsSource="{x:Static local:Window1.TreeNodeList}" /> What am I doing wrong here?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Give this blog post a look and see how it works. I'm using the HierarchicalDataTemplate to display folders also. http://karlshifflett.wordpress.com/2007/11/10/over-reaction-to-a-simple-wpf-explorer-tree/[^]

      Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

      Just a grain of sand on the worlds beaches.

      A 1 Reply Last reply
      0
      • L Lost User

        Give this blog post a look and see how it works. I'm using the HierarchicalDataTemplate to display folders also. http://karlshifflett.wordpress.com/2007/11/10/over-reaction-to-a-simple-wpf-explorer-tree/[^]

        Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

        Just a grain of sand on the worlds beaches.

        A Offline
        A Offline
        artwallacex
        wrote on last edited by
        #3

        Karl, that example is lazing loading a filesystem view into a treeview which is the opposite of what I'm doing. This is more a question about the treeview expander/collapser than about the task shown in the screenshots. Please take another quick look at this code, ignoring the C:\. TreeNode tn1 = new TreeNode(@"C:\"); TreeNode tn2 = new TreeNode(@"C:\"); TreeNode tn3 = new TreeNode(@"C:\"); TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); TreeNodeList.Add(tn1); // Works fine The first Add(tn1) produces an item with no expander. The 2nd does.

        L 2 Replies Last reply
        0
        • A artwallacex

          Karl, that example is lazing loading a filesystem view into a treeview which is the opposite of what I'm doing. This is more a question about the treeview expander/collapser than about the task shown in the screenshots. Please take another quick look at this code, ignoring the C:\. TreeNode tn1 = new TreeNode(@"C:\"); TreeNode tn2 = new TreeNode(@"C:\"); TreeNode tn3 = new TreeNode(@"C:\"); TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); TreeNodeList.Add(tn1); // Works fine The first Add(tn1) produces an item with no expander. The 2nd does.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          artwallacex wrote:

          TreeNodeList.Add(tn1); // Produces item with no expander!

          I think this is because there are no child TreeNodes and WPF is working correctly. After adding a child, the expander shows up.

          Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

          Just a grain of sand on the worlds beaches.

          A 1 Reply Last reply
          0
          • L Lost User

            artwallacex wrote:

            TreeNodeList.Add(tn1); // Produces item with no expander!

            I think this is because there are no child TreeNodes and WPF is working correctly. After adding a child, the expander shows up.

            Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

            Just a grain of sand on the worlds beaches.

            A Offline
            A Offline
            artwallacex
            wrote on last edited by
            #5

            But there are two child treenodes created by: tn1.Children.Add(tn2); tn1.Children.Add(tn3); They show up in the UI and if you doubleclick on the parent they will be displayed. The problem is the collapser is not drawn but the child elements are there.

            1 Reply Last reply
            0
            • A artwallacex

              Karl, that example is lazing loading a filesystem view into a treeview which is the opposite of what I'm doing. This is more a question about the treeview expander/collapser than about the task shown in the screenshots. Please take another quick look at this code, ignoring the C:\. TreeNode tn1 = new TreeNode(@"C:\"); TreeNode tn2 = new TreeNode(@"C:\"); TreeNode tn3 = new TreeNode(@"C:\"); TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); TreeNodeList.Add(tn1); // Works fine The first Add(tn1) produces an item with no expander. The 2nd does.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              TreeNodeList.Add(tn1); // Produces item with no expander!

              tn1.Children.Add(tn2);
              tn1.Children.Add(tn3);

              TreeNodeList.Add(tn1); // Works fine

              The first line of code TreeNodeList.Add(tn1); // Produces item with no expander! has no children at that point in time. The second line does.

              Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

              Just a grain of sand on the worlds beaches.

              A 1 Reply Last reply
              0
              • L Lost User

                TreeNodeList.Add(tn1); // Produces item with no expander!

                tn1.Children.Add(tn2);
                tn1.Children.Add(tn3);

                TreeNodeList.Add(tn1); // Works fine

                The first line of code TreeNodeList.Add(tn1); // Produces item with no expander! has no children at that point in time. The second line does.

                Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                Just a grain of sand on the worlds beaches.

                A Offline
                A Offline
                artwallacex
                wrote on last edited by
                #7

                So you're saying that the databinding is not supposed to take care of that? The child nodes must be added before the parent item is added to the list? And again, running this code: TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); without the 2nd Add will still show all of the child elements, just without the expander. Double clicking the parent item will draw the child elements and the parent & children will produce events just like the others in the list.

                modified on Thursday, April 3, 2008 12:27 PM

                L 1 Reply Last reply
                0
                • A artwallacex

                  So you're saying that the databinding is not supposed to take care of that? The child nodes must be added before the parent item is added to the list? And again, running this code: TreeNodeList.Add(tn1); // Produces item with no expander! tn1.Children.Add(tn2); tn1.Children.Add(tn3); without the 2nd Add will still show all of the child elements, just without the expander. Double clicking the parent item will draw the child elements and the parent & children will produce events just like the others in the list.

                  modified on Thursday, April 3, 2008 12:27 PM

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I think the problem is, I'm not looking at the same code you are. Can you post all the code?

                  Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                  Just a grain of sand on the worlds beaches.

                  A 1 Reply Last reply
                  0
                  • L Lost User

                    I think the problem is, I'm not looking at the same code you are. Can you post all the code?

                    Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                    Just a grain of sand on the worlds beaches.

                    A Offline
                    A Offline
                    artwallacex
                    wrote on last edited by
                    #9

                    Sure, here's the solution in a 100k zip file. http://www.crankedup.com/misc/wpf-treeview-notriangle.zip[^] When you launch it, press the "Fake" button. It now calls this code: TreeNode tn1 = new TreeNode("1"); TreeNode tn2 = new TreeNode("2"); TreeNode tn3 = new TreeNode("3"); TreeNode tn4 = new TreeNode("4"); TreeNode tn5 = new TreeNode("5"); TreeNodeList.Add(tn1); tn1.Children.Add(tn2); tn1.Children.Add(tn3); tn2.Children.Add(tn4); tn2.Children.Add(tn5); And produces this screenshot (I've expanded all the nodes in the shot): http://www.crankedup.com/misc/wpf-treeview-notriangle3.png[^] PS- Thanks for taking a little time to look at this.

                    L 1 Reply Last reply
                    0
                    • A artwallacex

                      Sure, here's the solution in a 100k zip file. http://www.crankedup.com/misc/wpf-treeview-notriangle.zip[^] When you launch it, press the "Fake" button. It now calls this code: TreeNode tn1 = new TreeNode("1"); TreeNode tn2 = new TreeNode("2"); TreeNode tn3 = new TreeNode("3"); TreeNode tn4 = new TreeNode("4"); TreeNode tn5 = new TreeNode("5"); TreeNodeList.Add(tn1); tn1.Children.Add(tn2); tn1.Children.Add(tn3); tn2.Children.Add(tn4); tn2.Children.Add(tn5); And produces this screenshot (I've expanded all the nodes in the shot): http://www.crankedup.com/misc/wpf-treeview-notriangle3.png[^] PS- Thanks for taking a little time to look at this.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Moved the adding of tn1 to the bottom and it works great.

                      private void button3_Click(object sender, RoutedEventArgs e)
                      {
                      DirectoryInfo junkDir = new DirectoryInfo(@"C:\");

                      TreeNode tn1 = new TreeNode("1");
                      TreeNode tn2 = new TreeNode("2");
                      TreeNode tn3 = new TreeNode("3");
                      TreeNode tn4 = new TreeNode("4");
                      TreeNode tn5 = new TreeNode("5");
                      
                      
                      tn1.Children.Add(tn2);
                      tn1.Children.Add(tn3);
                      
                      tn2.Children.Add(tn4);
                      tn2.Children.Add(tn5);
                      
                      
                      //move this here, works great
                      TreeNodeList.Add(tn1);
                      
                      //TreeNodeList.Add(tn1);
                      

                      }

                      Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                      Just a grain of sand on the worlds beaches.

                      A 1 Reply Last reply
                      0
                      • L Lost User

                        Moved the adding of tn1 to the bottom and it works great.

                        private void button3_Click(object sender, RoutedEventArgs e)
                        {
                        DirectoryInfo junkDir = new DirectoryInfo(@"C:\");

                        TreeNode tn1 = new TreeNode("1");
                        TreeNode tn2 = new TreeNode("2");
                        TreeNode tn3 = new TreeNode("3");
                        TreeNode tn4 = new TreeNode("4");
                        TreeNode tn5 = new TreeNode("5");
                        
                        
                        tn1.Children.Add(tn2);
                        tn1.Children.Add(tn3);
                        
                        tn2.Children.Add(tn4);
                        tn2.Children.Add(tn5);
                        
                        
                        //move this here, works great
                        TreeNodeList.Add(tn1);
                        
                        //TreeNodeList.Add(tn1);
                        

                        }

                        Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                        Just a grain of sand on the worlds beaches.

                        A Offline
                        A Offline
                        artwallacex
                        wrote on last edited by
                        #11

                        Yes, I know. But I've yet to see anything that suggests it shouldn't work the other way. To me it looks like a bug in the TreeView.

                        L 1 Reply Last reply
                        0
                        • A artwallacex

                          Yes, I know. But I've yet to see anything that suggests it shouldn't work the other way. To me it looks like a bug in the TreeView.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I found the solution. First put this back back the way it was.

                          private void button3_Click(object sender, RoutedEventArgs e)
                          {
                          DirectoryInfo junkDir = new DirectoryInfo(@"C:\");

                          TreeNode tn1 = new TreeNode("1");
                          TreeNode tn2 = new TreeNode("2");
                          TreeNode tn3 = new TreeNode("3");
                          TreeNode tn4 = new TreeNode("4");
                          TreeNode tn5 = new TreeNode("5");
                          
                          
                          TreeNodeList.Add(tn1);
                          
                          tn1.Children.Add(tn2);
                          tn1.Children.Add(tn3);
                          
                          tn2.Children.Add(tn4);
                          tn2.Children.Add(tn5);
                          

                          }

                          Modify the TreeNode class as below.

                          using System;
                          using System.Collections.Generic;
                          using System.IO;
                          using System.Linq;
                          using System.Text;
                          using System.ComponentModel;
                          using System.Collections.ObjectModel;

                          namespace WpfTest
                          {
                          public class TreeNode
                          {
                          private string dir = "";
                          private ObservableCollection<TreeNode> children = new ObservableCollection<TreeNode>();
                          private List<string> files = new List<string>();

                              public TreeNode(string directory)
                              {
                                  dir = directory;
                              }
                          
                              public string Dir
                              {
                                  get { return dir; }
                                  set { dir = value; }
                              }
                          
                              public ObservableCollection<TreeNode> Children
                              {
                                  get { return children; }
                                  set { children = value; }
                              }
                          
                              public List<string> Files
                              {
                                  get { return files; }
                                  set { files = value; }
                              }
                          }
                          

                          }

                          Your all done. You may want to do the same to the Files collection also.

                          Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                          Just a grain of sand on the worlds beaches.

                          A 1 Reply Last reply
                          0
                          • L Lost User

                            I found the solution. First put this back back the way it was.

                            private void button3_Click(object sender, RoutedEventArgs e)
                            {
                            DirectoryInfo junkDir = new DirectoryInfo(@"C:\");

                            TreeNode tn1 = new TreeNode("1");
                            TreeNode tn2 = new TreeNode("2");
                            TreeNode tn3 = new TreeNode("3");
                            TreeNode tn4 = new TreeNode("4");
                            TreeNode tn5 = new TreeNode("5");
                            
                            
                            TreeNodeList.Add(tn1);
                            
                            tn1.Children.Add(tn2);
                            tn1.Children.Add(tn3);
                            
                            tn2.Children.Add(tn4);
                            tn2.Children.Add(tn5);
                            

                            }

                            Modify the TreeNode class as below.

                            using System;
                            using System.Collections.Generic;
                            using System.IO;
                            using System.Linq;
                            using System.Text;
                            using System.ComponentModel;
                            using System.Collections.ObjectModel;

                            namespace WpfTest
                            {
                            public class TreeNode
                            {
                            private string dir = "";
                            private ObservableCollection<TreeNode> children = new ObservableCollection<TreeNode>();
                            private List<string> files = new List<string>();

                                public TreeNode(string directory)
                                {
                                    dir = directory;
                                }
                            
                                public string Dir
                                {
                                    get { return dir; }
                                    set { dir = value; }
                                }
                            
                                public ObservableCollection<TreeNode> Children
                                {
                                    get { return children; }
                                    set { children = value; }
                                }
                            
                                public List<string> Files
                                {
                                    get { return files; }
                                    set { files = value; }
                                }
                            }
                            

                            }

                            Your all done. You may want to do the same to the Files collection also.

                            Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                            Just a grain of sand on the worlds beaches.

                            A Offline
                            A Offline
                            artwallacex
                            wrote on last edited by
                            #13

                            Doh, very good catch. Unfortunately my real project is using an ObservableCollection with the proper using statements. The List<> is something I forgot or either experimented with in my sample project. So the sample project now works correctly so I'm back to hunting in my code. Thanks for your help Karl. Edit- My real project has the same problem. The collection bound to the list is an ObservableCollection but the children are just Lists, like in the sample. So double thanks Karl, you were spot on.

                            modified on Thursday, April 3, 2008 1:49 PM

                            L 1 Reply Last reply
                            0
                            • A artwallacex

                              Doh, very good catch. Unfortunately my real project is using an ObservableCollection with the proper using statements. The List<> is something I forgot or either experimented with in my sample project. So the sample project now works correctly so I'm back to hunting in my code. Thanks for your help Karl. Edit- My real project has the same problem. The collection bound to the list is an ObservableCollection but the children are just Lists, like in the sample. So double thanks Karl, you were spot on.

                              modified on Thursday, April 3, 2008 1:49 PM

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Glad to help out. I'm a VB.NET geek so digging into the C# code is good for me. :cool:

                              Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                              Just a grain of sand on the worlds beaches.

                              A 1 Reply Last reply
                              0
                              • L Lost User

                                Glad to help out. I'm a VB.NET geek so digging into the C# code is good for me. :cool:

                                Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                                Just a grain of sand on the worlds beaches.

                                A Offline
                                A Offline
                                artwallacex
                                wrote on last edited by
                                #15

                                Heh, it was funny that you posted your TreeView article at the beginning of this thread. It was through that article that I read your mentoring article, which was what got me trying to nail down the problem in this sample application (mentorees "Do your own homework"/"forces you to study and carefully review your topic")! It's a small world.

                                L 1 Reply Last reply
                                0
                                • A artwallacex

                                  Heh, it was funny that you posted your TreeView article at the beginning of this thread. It was through that article that I read your mentoring article, which was what got me trying to nail down the problem in this sample application (mentorees "Do your own homework"/"forces you to study and carefully review your topic")! It's a small world.

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  This means we are having a good day then. :cool: The homework business normally translates to, "please check Google, then post question." Just glad we got you on the right track.

                                  Cheers, Karl » CodeProject 2008 MVP » Microsoft MVP - Client App Dev My Blog | Mole's Home Page | MVP Profile

                                  Just a grain of sand on the worlds beaches.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups