Is WPF TreeView binding supposed to work like this (no collapse triangle)? [modified]
-
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.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 ProfileJust a grain of sand on the worlds beaches.
-
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 ProfileJust a grain of sand on the worlds beaches.
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
-
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
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 ProfileJust a grain of sand on the worlds beaches.
-
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 ProfileJust a grain of sand on the worlds beaches.
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. -
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.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 ProfileJust a grain of sand on the worlds beaches.
-
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 ProfileJust a grain of sand on the worlds beaches.
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.
-
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.
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 ProfileJust a grain of sand on the worlds beaches.
-
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 ProfileJust a grain of sand on the worlds beaches.
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
-
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
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 ProfileJust a grain of sand on the worlds beaches.
-
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 ProfileJust a grain of sand on the worlds beaches.
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.
-
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.
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 ProfileJust a grain of sand on the worlds beaches.