WPF 利用TreeView 树状体 和行政区节点

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();
                            。。。。。。。

我这边使用内套循环, 一层一层获取下去。

3.还发现了一个汉子转换拼音的小工具

https://www.cnblogs.com/chenjinshi/p/4522230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值