0
我有一個入門級:如何從數據庫中氮水平填補了分層樹形視圖
public class Entry
{
public int Key { get; set; }
public string Name { get; set; }
}
和組類:
public class Group
{
public int Key { get; set; }
public string Name { get; set; }
public List<Group> SubGroups { get; set; }
public List<Entry> Entries { get; set; }
public List<object> Items
{
get
{
List<object> childNodes = new List<object>();
foreach (var group in this.SubGroups)
childNodes.Add(group);
foreach (var entry in this.Entries)
childNodes.Add(entry);
return childNodes;
}
}
}
我可以將數據手工添加到TreeView的是這樣的:
public class TestData
{
public List<Group> Groups = new List<Group>();
public void Load()
{
Group grp1 = new Group() { Key = 1, Name = "Group 1", SubGroups = new List<Group>(), Entries = new List<Entry>() };
Group grp2 = new Group() { Key = 2, Name = "Group 2", SubGroups = new List<Group>(), Entries = new List<Entry>() };
Group grp3 = new Group() { Key = 3, Name = "Group 3", SubGroups = new List<Group>(), Entries = new List<Entry>() };
Group grp4 = new Group() { Key = 4, Name = "Group 4", SubGroups = new List<Group>(), Entries = new List<Entry>() };
//grp1
grp1.Entries.Add(new Entry() { Key = 1, Name = "Entry number 1" });
grp1.Entries.Add(new Entry() { Key = 2, Name = "Entry number 2" });
grp1.Entries.Add(new Entry() { Key = 3, Name = "Entry number 3" });
//grp2
grp2.Entries.Add(new Entry() { Key = 4, Name = "Entry number 4" });
grp2.Entries.Add(new Entry() { Key = 5, Name = "Entry number 5" });
grp2.Entries.Add(new Entry() { Key = 6, Name = "Entry number 6" });
//grp3
grp3.Entries.Add(new Entry() { Key = 7, Name = "Entry number 7" });
grp3.Entries.Add(new Entry() { Key = 8, Name = "Entry number 8" });
grp3.Entries.Add(new Entry() { Key = 9, Name = "Entry number 9" });
//grp4
grp4.Entries.Add(new Entry() { Key = 10, Name = "Entry number 10" });
grp4.Entries.Add(new Entry() { Key = 11, Name = "Entry number 11" });
grp4.Entries.Add(new Entry() { Key = 12, Name = "Entry number 12" });
Groups.Add(grp1);
Groups.Add(grp2);
Groups.Add(grp3);
Groups.Add(grp4);
grp1.SubGroups.Add(grp2);
grp2.SubGroups.Add(grp3);
}
及其負載數據類別
private void LoadView()
{
TestData data = new TestData();
data.Load();
GroupView.ItemsSource = data.Groups;
}
不過,我想從數據庫填充的TreeView像這樣的:
ID groupFlag fatherID Name
1 1 Null Group1
2 1 1 Group1.1
3 0 2 Entry1
4 0 2 Entry2
5 1 Null Group2
6 0 5 Entry3
任何幫助,將不勝感激......
我想你在尋找遞歸。你的Group是DB類?你最終想要什麼樣的結構? –
如果你有一個帶有一些數據的Group和Entry表的數據庫,你只需要一個sql命令。然後你可以建立一個sql連接並填充你的treeview。 –
Entry和Group都從DB讀取,表與它自己有關係,並且FatherID指向父組... – nadaram