1.TreeView 树状体
网上很多教程 这里我参考了 https://www.cnblogs.com/guxin/p/wpf-how-to-use-treeview-by-mvvm.html
前端控件:
<TreeView Name="tvProperty" Margin="10,100,10,10" Background="#007D7D7D" SelectedItemChanged="tvProperty_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16px" Height="16px" Margin="0,0,2,2"/>
<TextBlock VerticalAlignment="Center" Text="{Binding DisplayName}" Foreground="White" FontSize="14"/>
<StackPanel.ToolTip>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200px" FontSize="14"/>
</StackPanel.ToolTip>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
绑定实例:
internal class PropertyNodeItem
{
public string Icon { get; set; }
public string DisplayName { get; set; }
public string Name { get; set; }
public Community node { get; set; }
public List<PropertyNodeItem> Children { get; set; }
public PropertyNodeItem()
{
Children = new List<PropertyNodeItem>();
}
}
Provice_Node = new PropertyNodeItem()
{
Icon = Path.Combine(Config.GetCatalog("PATH_IMAGE_LAYER"), "folder.png"),
DisplayName = "空",
Name = "空"
};
City_Node = new PropertyNodeItem()
{
Icon = Path.Combine(Config.GetCatalog("PATH_IMAGE_LAYER"), "folder.png"),
DisplayName = "空",
Name = "空"
};
Provice_Node.Children.Add(City_Node);
listItem.Add(Provice_Node);
这样就可以了,可以加照片什么的
## 2.List 分组 构建行政区节点
https://blog.csdn.net/zhangxiao0122/article/details/88570472 分组 参考 该文章,
中间使用 list.GourpBy 的时候一直 报错, 这时候 引用下 using system.Linq; 就可以了,具体为什么, 也不知道
Provice_Node = new PropertyNodeItem()
{
Icon = Path.Combine(Config.GetCatalog("PATH_IMAGE_LAYER"), "folder.png"),
DisplayName = item.proviceName,
Name = item.proviceName,
};
List<GroupByCityCode> CityList = new List<GroupByCityCode>();
CityList = item.List.GroupBy(x => new { x.cityCode, x.cityName }).Select(group => new GroupByCityCode
{
cityCode = group.Key.cityCode,
cityName = group.Key.cityName,
List = group.ToList()
}).ToList();
foreach (var city in CityList)
{
#region 获取市列表
City_Node = new PropertyNodeItem()
{
Icon = Path.Combine(Config.GetCatalog("PATH_IMAGE_LAYER"), "folder.png"),
DisplayName = city.cityName,
Name = city.cityName,
};
List<GroupByDistinctCode> DistinctList = new List<GroupByDistinctCode>();
DistinctList = city.List.GroupBy(x => new { x.distinctCode, x.distinctName }).Select(group => new GroupByDistinctCode
{
distinctCode = group.Key.distinctCode,
distinctName = group.Key.distinctName,
List = group.ToList()
}).ToList();
。。。。。。。
我这边使用内套循环, 一层一层获取下去。