WPF—TreeView无限极绑定集合形成树结构

简介: 原文:WPF—TreeView无限极绑定集合形成树结构1.如图所示:绑定树效果图   2.前台Xaml代码:                                                                                          ...
原文: WPF—TreeView无限极绑定集合形成树结构

1.如图所示:绑定树效果图

 

2.前台Xaml代码:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mode="clr-namespace:WpfTest"
        Title="TreeView无限级树绑定事例" Height="300" Width="300" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid>
        <Grid.Resources>
            <HierarchicalDataTemplate DataType="{x:Type mode:Node}" ItemsSource="{Binding Nodes}">
                <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                    <!--<Image Source="pack://application:,,,/WpfTest;Component/Resources/KnowDot.png" Width="16" Height="16" />-->
                    <!--<Image Source="Resources/KnowDot.png" Width="16" Height="16" />-->
                    <Image Source="/WpfTest;Component/Resources/KnowDot.png" Width="16" Height="16" />
                    <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </Grid.Resources>
        <TreeView Name="TreeView"/>
    </Grid>
</Window>

 

2.后台cs代码:采用递归无限极向下查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;
using System.Windows.Markup;


namespace WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Node> nodes = new List<Node>()
            {
                new Node { ID = 1, Name = "中国" },
                new Node { ID = 2, Name = "北京市", ParentID = 1 },
                new Node { ID = 3, Name = "吉林省", ParentID = 1 },
                new Node { ID = 4, Name = "上海市", ParentID = 1 },
                new Node { ID = 5, Name = "海淀区", ParentID = 2 },
                new Node { ID = 6, Name = "朝阳区", ParentID = 2 },
                new Node { ID = 7, Name = "大兴区", ParentID = 2 },
                new Node { ID = 8, Name = "白山市", ParentID = 3 },
                new Node { ID = 9, Name = "长春市", ParentID = 3 },
                new Node { ID = 10, Name = "抚松县", ParentID = 8 },
                new Node { ID = 11, Name = "靖宇县", ParentID = 8 }
            };
            // 绑定树
            List<Node> outputList = Bind(nodes);
            //(TreeView.SelectedItem as Node).ID
            this.TreeView.ItemsSource = outputList;
            //TreeViewItem item = new TreeViewItem();
            //item.Header = "";
        }
        /// <summary>
        /// 绑定树
        /// </summary>
        List<Node> Bind(List<Node> nodes)
        {
            List<Node> outputList = new List<Node>();
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].ParentID == -1)
                {
                    outputList.Add(nodes[i]);
                }
                else
                {
                    FindDownward(nodes, nodes[i].ParentID).Nodes.Add(nodes[i]);
                }
            }
            return outputList;
        }
        /// <summary>
        /// 递归向下查找
        /// </summary>
        Node FindDownward(List<Node> nodes, int id)
        {
            if (nodes == null) return null;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].ID == id)
                {
                    return nodes[i];
                }
                Node node = FindDownward(nodes[i].Nodes, id);
                if (node != null)
                {
                    return node;
                }
            }
            return null;
        }
    }

    public class Node
    {
        public Node()
        {
            this.Nodes = new List<Node>();
            this.ParentID = -1;
        }
        public int ID { get; set; }
        public string Name { get; set; }
        public int ParentID { get; set; }
        public List<Node> Nodes { get; set; }
    }
}

目录
相关文章
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
332 0
WPF—多重绑定和跨层级绑定
WPF—多重绑定和跨层级绑定
|
3月前
|
开发框架 前端开发 JavaScript
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(5) -- 树列表TreeView的使用
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(5) -- 树列表TreeView的使用
|
3月前
|
开发框架 前端开发 JavaScript
在WPF应用中使用GongSolutions.WPF.DragDrop实现列表集合控件的拖动处理
在WPF应用中使用GongSolutions.WPF.DragDrop实现列表集合控件的拖动处理
|
C# 数据格式 XML
WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
8401 0
|
数据可视化 C#
WPF技术之TreeView控件
WPF TreeView控件是一个用于显示分层数据的控件,类似于文件浏览器中的目录结构。
170 0
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
166 0
|
C#
WPF更新绑定字段
WPF更新绑定字段
101 0
|
前端开发 C#
WPF 之 数据与命令绑定 (MVVM方式)
WPF 之 数据与命令绑定 (MVVM方式)
195 0
WPF 之 数据与命令绑定 (MVVM方式)
|
前端开发 C#
WPF TreeView 启动时如何默认选择首个节点
WPF TreeView 启动时如何默认选择首个节点