zoukankan      html  css  js  c++  java
  • WPF Tree多级绑定

    <Window x:Class="TreeTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TreeTest"
            mc:Ignorable="d"
            Title="MainWindow" Height="650" Width="400">
        <Grid>
            <Grid.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local: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="/TreeTest;Component/Resources/KnowDot.png" Width="16" Height="16" />
                        <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="150"/>
            </Grid.RowDefinitions>
            <TreeView Name="TreeView"/>
            <TextBox Grid.Row="1" DataContext="{Binding ElementName=TreeView, Path=SelectedItem}" Text="{Binding Desp, Mode=TwoWay}">
                
                
            </TextBox>
            <Button Grid.Row="1" Width="100" Margin="282,10,10,-10" Click="Button_Click"></Button>
        </Grid>
    </Window>
    using System.Collections.Generic;
    using System.Windows;
    
    namespace TreeTest
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            List<Node> outputList = new List<Node>();
            public MainWindow()
            {
                InitializeComponent();
                List<Node> nodes = new List<Node>()
                {
                    new Node { ID = 1, Name = "中国" },
                    new Node { ID = 2, Name = "北京市", ParentID = 1, Desp = "北京描述" },
                    new Node { ID = 3, Name = "吉林省", ParentID = 1, Desp = "吉林描述"  },
                    new Node { ID = 4, Name = "上海市", ParentID = 1 , Desp = "上海描述" },
                    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 },
                    new Node { ID = 12, Name = "美国" },
                    new Node { ID = 13, Name = "南美洲", ParentID = 12},
                    new Node { ID = 14, Name = "纽约", ParentID = 13 },
                };
                // 绑定树
              outputList = Bind(nodes);
                //(TreeView.SelectedItem as Node).ID
                this.TreeView.ItemsSource = outputList;
            }
            /// <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;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                int a = 3;
            }
        }
    
        public class Node
        {
            public Node()
            {
                this.Nodes = new List<Node>();
                this.ParentID = -1;
                Desp = "描述";
            }
            public int ID { get; set; }
            public string Name { get; set; }
            public int ParentID { get; set; }
            public string Desp { get; set; }
            public List<Node> Nodes { get; set; }
        }
    }
  • 相关阅读:
    io工具类
    并发高级知识
    HashMap相关源码阅读
    ArrayList和LinkedList部分源码分析性能差异
    我自己的JdbcTemplate
    mysql5.7.20靠谱安装步骤
    NG 转发配置
    SQLite总结
    算是不常用的东西,java中的ResultSet转List
    不常用的技能-【手动编译java类】
  • 原文地址:https://www.cnblogs.com/3xiaolonglong/p/14081336.html
Copyright © 2011-2022 走看看