zoukankan      html  css  js  c++  java
  • wpf Tree

    code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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;
    
    namespace CodeAI
    {
        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; }
        }  
    
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                new TemplateControl().load();
    
                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);
    
                this.TreeView.ItemsSource = outputList;   
            }
            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;
            }
    
            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 TextBlock_MouseDown_1(object sender, MouseButtonEventArgs e)
            {
    
            }   
        }
    }
    View Code

    xaml

    <Window x:Class="CodeAI.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:src="clr-namespace:CodeAI"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <HierarchicalDataTemplate DataType="{x:Type src: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}" MouseDown="TextBlock_MouseDown_1"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </Grid.Resources>
            <TreeView Name="TreeView"/>
            
        </Grid>
    </Window>
    View Code

    树形版

    code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CodeAI
    {
        public class TemplateNode
        {
            public TemplateNode()
            {
                this.childs = new List<TemplateNode>();
            }
            public string Name { get; set; }
    
            private TemplateNode parent;
            public TemplateNode Parent
            {
                set
                {
                    value.childs.Add(this);
                    parent = value;
                }
                get
                {
                    return parent;
                }
            }
            public List<TemplateNode> childs { get; set; }
    
            public void addChild(TemplateNode node)
            {
                node.parent = this;
                childs.Add(node);
            }
            public string filePath;
    
            public bool PictureVisible
            {
                get{
                    if(parent != null && parent.parent == null){
                        return true;
                    }
                    return false;
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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;
    
    namespace CodeAI
    {
        /// <summary>
        /// TemplateControl.xaml 的交互逻辑
        /// </summary>
        public partial class TemplateControl : UserControl
        {
            public TemplateControl()
            {
                InitializeComponent();
            }
    
            public void load()
            {
                TemplateNode root = new TemplateNode();
                root.Name = "root";
                if(Directory.Exists("template"))
                {
                    loadSub("template", root);
                    string[] subFolder = Directory.GetDirectories("template");
                }
            }
    
            public void loadSub(string folderPath, TemplateNode parent)
            {
                string[] subFolders = Directory.GetDirectories(folderPath);
    
                foreach (string filePath in Directory.GetFiles(folderPath))
                {
                    TemplateNode node = new TemplateNode();
                    node.filePath = filePath;
                    node.Name = System.IO.Path.GetFileName(filePath);
                    node.Parent = parent;
                }
    
                foreach (string subFolder in subFolders)
                {
                    TemplateNode subParent = new TemplateNode();
                    subParent.filePath = subFolder;
                    subParent.Name = System.IO.Path.GetDirectoryName(subFolder);
                    subParent.Parent = parent;
    
                    loadSub(folderPath , subParent);
                }
            }
        }
    }
    View Code

    xaml

    <UserControl x:Class="CodeAI.TemplateControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 xmlns:src="clr-namespace:CodeAI"
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <HierarchicalDataTemplate DataType="{x:Type src:TemplateNode}" ItemsSource="{Binding childs}">
                <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                    <Image Source="image/folder.png" Visibility="{Binding PictureVisible}" Width="16" Height="16" />
                    <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </UserControl.Resources>
    
        <Grid>
            <TreeView Name="TreeView"/>
        </Grid>
    </UserControl>
    View Code

    节点事件

    使用模板HierarchicalDataTemplate

    复制代码
    <HierarchicalDataTemplate x:Key="BookMarkTemplate" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Child.MarkName,Mode=TwoWay}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate x:Key="ChapterTemplate" ItemTemplate="{StaticResource BookMarkTemplate}" ItemsSource="{Binding InlineList,Mode=TwoWay}" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
    复制代码

    从父节点删除选中项

      <TreeView  x:Name="treeview"  TreeViewItem.Selected="treeView1_Selected" ItemTemplate="{StaticResource ChapterTemplate}">

    复制代码
    private void treeView1_Selected(object sender, RoutedEventArgs e)
            {
                if ((e.OriginalSource as TreeViewItem).Header.GetType()==typeof(InlineUIContainer))
                {
                    mark = ((e.OriginalSource as TreeViewItem).Header as InlineUIContainer);
                    BookMarkRun run = mark.Child as BookMarkRun;
                    txtSelectionContent.Text = run.MarkName;
                    DependencyObject parent = VisualTreeHelper.GetParent((e.OriginalSource as TreeViewItem));
                    while (!(parent is TreeViewItem))
                        parent = VisualTreeHelper.GetParent(parent);
                    TreeViewItem item = (TreeViewItem)parent;
                    volumeModel = (item.Header as VolumeModel);
                    btnAdd.Content = "修改";
                    btnDel.IsEnabled = true;
                    btnAdd.IsEnabled = true;
                }
         }
    复制代码
  • 相关阅读:
    Caliburn micro 学习笔记...
    First steps with Caliburn Micro in Windows Phone 8 系列文章
    WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开
    http各个状态码的详解
    点阵字库产生的原理
    Windows 服务调试方法(基于.net framwork4.6)
    关于.net Core 笔记
    JS+ google.maps.api 实现基本的导航功能
    C# 遍历控件检查是否有被选中的项(通用)
    C#编程习惯
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3754106.html
Copyright © 2011-2022 走看看