使用DataTable FertSt = dbConn.GetFertSt();
,我得到以下结果:
FertSt | MatNr
BAA | 123
BAA | 234
BAB | 111
BAC | 123
BBA | 234
BBB | 111
BBB | 123
BCA | 234
BCD | 111
CAA | 123
CAA | 234
CAB | 111
现在,我想使用以下复选框将TreeView存档:
B
BA
BAA
123
234
BAB
111
BAC
123
BB
BBA
234
...
C
要创建TreeView,我使用了本教程:https://www.codeproject.com/Articles/28306/Working-with-Checkboxes-in-the-WPF-TreeView
但是我如何填充和排序带有子项的树视图?!
这是我如何对其进行硬编码的填充,但是我想从数据库中获取数据:
FilterViewModel root = new FilterViewModel("B")
{
//IsInitiallySelected = true,
Children =
{
new FilterViewModel("BA")
{
Children =
{
new FilterViewModel("BAA"){
Children =
{
new FilterViewModel("111111"),
new FilterViewModel("111112"),
new FilterViewModel("111113"),
new FilterViewModel("111114"),
}
},
new FilterViewModel("BAB"){
Children =
{
new FilterViewModel("211111"),
new FilterViewModel("211112"),
new FilterViewModel("211114"),
}
},
new FilterViewModel("BAC"),
}
},
new FilterViewModel("BC")
{
Children =
{
new FilterViewModel("BCI"){
Children =
{
new FilterViewModel("112111"),
new FilterViewModel("111112"),
new FilterViewModel("113113"),
new FilterViewModel("111514"),
}
},
}
},
new FilterViewModel("BD")
{
Children =
{
new FilterViewModel("BDA"),
new FilterViewModel("BDB"),
new FilterViewModel("BDC"),
}
},
new FilterViewModel("BE")
{
Children =
{
new FilterViewModel("BEA"),
new FilterViewModel("BEB"),
new FilterViewModel("BEC"),
}
},
}
};
答案 0 :(得分:0)
假设您可以将数据库中的内容加载到FilterViewModel的列表中,然后可以使用一系列字典来构建层次结构。
var level1Items = new Dictionary<string, FilterViewModel>();
var level2Items = new Dictionary<string, FilterViewModel>();
var level3Items = new Dictionary<string, FilterViewModel>();
Foreach(var item in FilterViewModelList)
{
var level1Key = item.FertSt.Substring(0,1);
if (!level1Items.ContainsKey(level1Key))
level1Items[level1Key] = new FilterViewModel(level1Key);
var level1Item = level1Items[level1Key];
var level2Key = item.FertSt.Substring(0,2);
if (!level2Items.ContainsKey(level2Key))
{
level2Items[level2Key] = new FilterViewModel(level2Key);
level1Item.Children.Add(level2Items[level2Key]);
}
var level2Item = level2Items[level2Key];
var level3Key = item.FertSt.Substring(0,3);
if (!level3Items.ContainsKey(level3Key))
{
level3Items[level3Key] = new FilterViewModel(level3Key);
level2Item.Children.Add(level3Items[level3Key]);
}
var level3Item = level3Items[level3Key];
level3Item.Children.Add(item);
}
然后可以将level1Items.Values
用作ItemsSource
的{{1}}。