我正在尝试制作一个按以下顺序包含项目的树视图。
+ Continent
+ Country
+ Province
+ Territory
而且,我有一个通用类来保存 treeView Items 数据。
public class TreeViewContinents
{
public List<TreeViewContinents> Children { get; set; }
public TreeViewContinents Parent { get; set; }
public string Name { get; set; }
public string Content { get; set; }
}
数据:
private readonly List<Dictionary<string, object>> continentsList = new List<Dictionary<string, object>>();
/*
continentsList[0] - "Continent", "Asia"
"Country", "Afghanistan"
"Province", "Kabul"
"Territory", "Bagrami"
"Language", "aaa"
"Culture", "bbb"
.
.
.
.
continentsList[n] - "Continent", "North America"
"Country", "Canada"
"Province", "Ontario"
"Territory", "Ottawa"
"Language", "aaa"
"Culture", "bbb
*/
基于需要生成哪些treeView Items 的字符串数组。
var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };
代码:
List<object> Continents = this.continentsList.Where(
p_oDict => p_oDict.ContainsKey(treeViewItemNames[0]))
.Select( p_oDict => p_oDict[treeViewItemNames[0]])
.Distinct()
.ToList();
var topItems = new List<TreeViewContinents>();
foreach (object continent in Continents)
{
var level1Items = new TreeViewContinents { Name = treeViewItemNames[0], Content = continent.ToString() };
List<object> Countries = this.continentsList.Where(
p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
&& p_oDict.ContainsKey(treeViewItemNames[1])
&& p_oDict[treeViewItemNames[0]].Equals(continent)
.Select( p_oDict => p_oDict[treeViewItemNames[1]])
.Distinct()
.ToList();
Countries.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
foreach (object country in Countries)
{
var level2Items = new TreeViewContinents { Name = treeViewItemNames[1], Content = country.ToString(), Parent = level1Items };
level1Items.Children.Add(level2Items);
List<object> Provinces = this.continentsList.Where(
p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
&& p_oDict.ContainsKey(treeViewItemNames[1])
&& p_oDict.ContainsKey(treeViewItemNames[2])
&& p_oDict[treeViewItemNames[0]].Equals(continent)
&& p_oDict[treeViewItemNames[1]].Equals(country))
.Select( p_oDict => p_oDict[treeViewItemNames[2]]).Distinct()
.ToList();
Provinces.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
foreach (object province in Provinces)
{
var level3Items = new TreeViewContinents { Name = treeViewItemNames[2], Content = province.ToString(), Parent = level2Items };
level2Items.Children.Add(level3Items);
List<object> Territories = this.continentsList.Where(
p_oDict => p_oDict.ContainsKey(treeViewItemNames[0])
&& p_oDict.ContainsKey(treeViewItemNames[1])
&& p_oDict.ContainsKey(treeViewItemNames[2])
&& p_oDict.ContainsKey(treeViewItemNames[3])
&& p_oDict[treeViewItemNames[0]].Equals(continent)
&& p_oDict[treeViewItemNames[1]].Equals(country)
&& p_oDict[treeViewItemNames[2]].Equals(province)).Select(
p_oDict => p_oDict[treeViewItemNames[3]])
.Distinct()
.ToList();
Territories.Sort((p_oU1, p_oU2) => string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
foreach (object territory in Territories)
{
var level4Items = new TreeViewContinents { Name = treeViewItemNames[3], Content = territory.ToString(), Parent = level3Items };
level3Items.Children.Add(level4Items);
}
}
}
topItems.Add(level1Items);
}
在上面的代码中,我有四个 foreach 循环来构建树视图。是否可以编写一个循环来基于变量生成树视图treeViewItemNames
,以便将来如果我更改变量
var treeViewItemNames = new[] { "Continent", "Country", "Territory" };
它应该像这样生成treeView
+ Continent
+ Country
+ Territory
请提出想法/建议。