• wpf之TreeView


    <Window x:Class="MyWpf.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:MyWpf"
            mc:Ignorable="d" 
            Title="MainWindow" Height="296.042" Width="347.333">
    
        <Grid>
            <TreeView HorizontalAlignment="Left" Height="247" Margin="10,10,0,0" VerticalAlignment="Top" Width="135">
                <!--IsExpanded=true表示展开节点 -->
                <TreeViewItem Header="学生管理系统" IsExpanded="True">
                    <!-- IsSelected是否被选中-->
                    <TreeViewItem Header="系统管理系统" IsExpanded="True" IsSelected="True">
                        <TreeViewItem Header="角色管理系统" IsExpanded="True">
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>
    
            </TreeView>
    
        </Grid>
    </Window>

    上面是静态简单生成一个树形结构

     结果:

     下面是一个非常简单的动态加载的树形:

    <Window x:Class="MyWpf.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:MyWpf"
            mc:Ignorable="d" 
            Title="MainWindow" Height="296.042" Width="347.333" Loaded="Window_Loaded">
    
        <Grid>
            <TreeView x:Name="tvList" HorizontalAlignment="Left"  MinHeight="247" Height="auto" Margin="10,10,0,0" VerticalAlignment="Top" Width="90">
                <!--IsExpanded=true表示展开节点 -->
               
    
            </TreeView>
    
        </Grid>
    </Window>

    后台代码:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent(); 
            }
            
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                //代码添加
                TreeViewItem ti = new TreeViewItem();
                ti.Header = "基础信息管理系统";
                //设置信息悬浮提示
                ti.ToolTip = ti.Header;
                tvList.Items.Add(ti);
    
                TreeViewItem ti1 = new TreeViewItem();
                ti1.Header = "用户管理系统";
                ti1.ToolTip = ti1.Header;
                ti.Items.Add(ti1);
                TreeViewItem ti2 = new TreeViewItem();
                ti2.Header = "角色管理系统";
                ti2.ToolTip = ti2.Header;
                ti.Items.Add(ti2);
            }
        }

     结果:

     下面是项目开发中通常用的,模型是自定义的:

    <Window x:Class="MyWpf.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:MyWpf"
            xmlns:model="clr-namespace:MyWpf"
            mc:Ignorable="d" 
            Title="MainWindow" Height="296.042" Width="347.333" Loaded="Window_Loaded">
    
        <Grid>
            <TreeView x:Name="tvList" HorizontalAlignment="Left"  MinHeight="247" Height="auto" Margin="10,10,0,0" VerticalAlignment="Top" Width="90"
                    ItemsSource="{Binding TreeList}" ToolTip="sd" > 
                <TreeView.ItemTemplate>
                    <!--DataType中使用情况的前提是引入了TreeViewTestModel模型,在这里是在上面通过 
                    xmlns:model="clr-namespace:MyWpf引入的" -->
                    <HierarchicalDataTemplate DataType="{x:Type model:TreeViewTestModel}"
                                              ItemsSource="{Binding Items}">
                        <!-- ToolTip加悬浮提示-->
                        <TextBlock Text="{Binding Name}" ToolTip="{Binding Name}"> 
                        </TextBlock>
                    </HierarchicalDataTemplate> 
                    
                </TreeView.ItemTemplate>
    
            </TreeView>
    
        </Grid>
    </Window>

     后台:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    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 MyWpf
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                List<TreeViewTestModel> list = new List<TreeViewTestModel>();
    
                TreeViewTestModel tree = new TreeViewTestModel();
                tree.Id = "1"; 
                tree.Name = "系统01";
                tree.ToolTip = tree.Name;
                TreeViewTestModel tree2 = new TreeViewTestModel();
                tree2.Id = "2";
                tree2.Name = "系统02";
                tree2.ToolTip = tree2.Name;
                tree.Items.Add(tree2);
    
                TreeViewTestModel tree3 = new TreeViewTestModel();
                tree3.Id = "3";
                tree3.Name = "系统03";
                tree3.ToolTip = tree3.Name;
                tree.Items.Add(tree3);
                list.Add(tree);
                TLModel tL = new TLModel();
                tL.TreeList = list;
                this.DataContext = tL;
               
            }
        }
    
        public class TLModel
        {
            public List<TreeViewTestModel> TreeList
            {
                get; set;
            }
        }
    
        public class TreeViewTestModel 
        {
            public TreeViewTestModel()
            {
                Items = new List<TreeViewTestModel>();
            }
            public string Id { get; set; }
    
            public string ToolTip { get; set; }
    
            public string Name { get; set; }
            public List<TreeViewTestModel> Items { get; set; }
        }
    
     
    }

    WPF使用模板HierarchicalDataTemplate实现TreeView层次显示

  • 相关阅读:
    百度多图上传
    uploadify--上传文件控件
    JS获取时间
    CSS选择器
    派大星博客的美化之路
    百度地图--JS版
    css实现元素下出现横线动画
    盒模型显隐、定位与流式布局思想
    css进度条
    Build Sharepoint 2013 Farm
  • 原文地址:https://www.cnblogs.com/anjingdian/p/15456112.html
Copyright © 2020-2023  润新知