Click here to Skip to main content
14,732,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class like this:
public class EmpCtg
	public int ID { get; set; }
	public string Name { get; set; }
	public EmpCtg Parent { get; set; }

Collection of this class:
List<EmpCtg> ListEmpCtg = //....get list<empctg>

I want binding ListEmpCtg to TreeView, and show look like this:
|          |
|          |
|          --------Ctg3
|          |
|          |
|          --------Ctg4
|                    |
|                    |
|                    --------Ctg5

I know that I can write in the code by hand, but I would like to know whether to use DataTemplate?

What should I do?

Updated 18-May-10 8:28am
yuzifu 18-May-10 22:35pm
Why does my post does not appear in the list?

1 solution


until now I've never used a TreeView, but while learning, I came across a few samples. They all used data objects that contained a link to the object's children (opposite to your class). Such a structure can be bound to the "ItemsSource"-property in a HierarchicalDataTemplate. To me it looks very reasonable to use 'child-oriented' data, as the tree is accessed from the top.

If your question was not only based on theoretical interest, you might consider storing your data in an
X(ml)Document (or any other fitting structure) and use it as shown here[^]. Maybe it's sufficient to add another member
public List<EmpCtg> Children;

to your class (as I said, I never used it myself).

Or you take a look at Simplifying the WPF TreeView...[^] from Josh Smith here on CodeProject (worth the time - anyway).


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