2021-08-23 WPF控件专题 TreeView控件详解

Posted 微软MVP Eleven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-08-23 WPF控件专题 TreeView控件详解相关的知识,希望对你有一定的参考价值。

一:静态使用

1.TreeView控件介绍

TreeView简介 :父类:ItemsControl —条目控件 MenuItem:HeaderedItemsControl

按层次结构组织数据 TreeViewItem —HeaderedItemsControl

属性介绍
SelectedItem 选择的项 SelectedValue 选择项的值
SelectedValuePath (string) 菜单名称 编号(列名或属性名)
ItemsSource 数据源

TreeViewItem介绍

TreeViewItem —HeaderedItemsControl
ItemsSource IsExpanded true 展开 false(默认) 折叠
IsSelected 是否选择

2.具体案例

<Window x:Class="WpfAppTest.TreeViewWindow"
        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:WpfAppTest"
        mc:Ignorable="d"
        Title="TreeViewWindow" Height="450" Width="800">
        <Window.Resources>
                <Style  TargetType="TreeViewItem">
                        <Setter Property="Foreground" Value="Orange"/>
                        <Setter Property="FontSize" Value="14"/>
                        <Setter Property="Padding" Value="10"/>
                        <Setter Property="Background" Value="Transparent"/>
                      
                </Style>
        </Window.Resources>
        <Grid>
                <TreeView Name="tvLsit" HorizontalAlignment="Left" Height="292" Margin="31,25,0,0" VerticalAlignment="Top" Width="330"  SelectedItemChanged="TvLsit_SelectedItemChanged">
                      
                        <TreeViewItem Header="学生管理系统" IsExpanded="True" >
                                <TreeViewItem Header="系统管理" IsExpanded="True">
                                        <TreeViewItem Header="角色管理" IsSelected="True" Foreground="LightBlue"  />
                                        <TreeViewItem Header="用户管理"/>
                                        <TreeViewItem Header="菜单管理"/>
                                        <TreeViewItem Header="权限管理"/>
                                </TreeViewItem>
                                <TreeViewItem Header="业务管理" IsExpanded="False">
                                        <TreeViewItem Header="采购入库"/>
                                        <TreeViewItem Header="销售出库"/>
                                </TreeViewItem>
                        </TreeViewItem>
                </TreeView>

        </Grid>
</Window>

/// <summary>
/// 节点选择响应事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TvLsit_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
        var val = e.NewValue as TreeViewItem;
        MessageBox.Show(val.Header.ToString());
}

二:动态使用

1.TreeView控件介绍

代码创建节点:可后台代码添加节点,并不比xaml代码简洁

多层次数据,构造出来:数据库表、Xml文档、Excel数据
编号 名称 父节点编号

1 进销存系统 0
2 系统管理 1
3 用户管理 2

动态生成节点树—分层数据模板

DataType 数据模型
ItemsSource 下一级别的数据列表

2.具体案例

<Window x:Class="WpfAppTest.TreeViewWindow2"
        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:WpfAppTest"
                xmlns:model="clr-namespace:WpfAppTest.Models"
        mc:Ignorable="d"
        Title="TreeViewWindow2" Height="450" Width="800" Loaded="Window_Loaded">
        <Grid>
                <TreeView Name="tvList" HorizontalAlignment="Left" Height="210" Margin="55,78,0,0" VerticalAlignment="Top" Width="283" ItemsSource="{Binding MenuList}" SelectedItemChanged="TvList_SelectedItemChanged">
                        <TreeView.ItemTemplate>
                                <HierarchicalDataTemplate DataType="{x:Type model:MenuItemModel}" ItemsSource="{Binding SubItems}">
                                        <TextBlock Text="{Binding MenuName}" VerticalAlignment="Center" />
                                </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                </TreeView>

        </Grid>
</Window>

namespace WpfAppTest
{
        /// <summary>
        /// TreeViewWindow2.xaml 的交互逻辑
        /// </summary>
        public partial class TreeViewWindow2 : Window
        {
                public TreeViewWindow2()
                {
                        InitializeComponent();
                }

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        //代码添加节点---并不比xaml代码简洁
                        {
                                //TreeViewItem ti = new TreeViewItem();
                                //ti.Header = "进销存管理系统";
                                //tvList.Items.Add(ti);

                                根节点下添加三个子节点
                                //TreeViewItem ti1 = new TreeViewItem();
                                //ti1.Header = "系统管理";
                                //ti.Items.Add(ti1);
                                //TreeViewItem ti2 = new TreeViewItem();
                                //ti2.Header = "业务管理";
                                //ti.Items.Add(ti2);
                                //TreeViewItem ti3 = new TreeViewItem();
                                //ti3.Header = "查询中心";
                                //ti.Items.Add(ti3);

                                系统管理节点下添加4个子节点
                                //TreeViewItem ti11 = new TreeViewItem();
                                //ti11.Header = "用户管理";
                                //ti1.Items.Add(ti11);
                                //TreeViewItem ti12 = new TreeViewItem();
                                //ti12.Header = "角色管理";
                                //ti1.Items.Add(ti12);
                                //TreeViewItem ti13 = new TreeViewItem();
                                //ti13.Header = "菜单管理";
                                //ti1.Items.Add(ti13);
                                //TreeViewItem ti14 = new TreeViewItem();
                                //ti14.Header = "权限管理";
                                //ti1.Items.Add(ti14);
                        }

                        {
                                List<MenuInfo> allMenus = GetMenuList();//基础菜单数据
                                List<MenuItemModel> menuList = new List<MenuItemModel>();//目标菜单数据
                                AddAllMenus(allMenus, menuList, null, 0);
                                VMenuModel vmodel = new VMenuModel();
                                vmodel.MenuList = menuList;
                                this.DataContext = vmodel;//当前Window的数据上下文
                        }
           
                }


                /// <summary>
                /// 递归加载菜单项数据
                /// </summary>
                /// <param name="allMenus"></param>
                /// <param name="menusList"></param>
                /// <param name="pMenu"></param>
                /// <param name="parentId"></param>
                private void AddAllMenus(List<MenuInfo> allMenus, List<MenuItemModel> menusList, MenuItemModel pMenu, int parentId)
                {
                        var subList = allMenus.Where(m => m.ParentId == parentId);
                        foreach (var mi in subList)
                        {
                                MenuItemModel miInfo = new MenuItemModel();
                                miInfo.MenuId = mi.MenuId;
                                miInfo.MenuName = mi.MenuName;
                                if (pMenu != null)
                                        pMenu.SubItems.Add(miInfo);
                                else
                                        menusList.Add(miInfo);
                                AddAllMenus(allMenus, menusList, miInfo, mi.MenuId);
                        }
                }

                /// <summary>
                /// 获取菜单数据
                /// </summary>
                /// <returns></returns>
                private List<MenuInfo> GetMenuList()
                {
                        string sql = "select MenuId,MenuName,ParentId from MenuInfos";
                        SqlDataReader dr = SqlHelper.ExecuteReader(sql, 1);
                        List<MenuInfo> list = new List<MenuInfo>();
                        while (dr.Read())
                        {
                                MenuInfo menu = new MenuInfo();
                                menu.MenuId = (int)dr["MenuId"];
                                menu.MenuName = dr["MenuName"].ToString();
                                menu.ParentId = (int)dr["ParentId"];
                                list.Add(menu);
                        }
                        dr.Close();
                        return list;
                }

                private void TvList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
                {
                        //响应操作
                        var val = e.NewValue as MenuItemModel;//
                        MessageBox.Show(val.MenuName);
                }
        }
}
public class VMenuModel
{
        /// <summary>
        /// Menu控件的数据源属性
        /// </summary>
        public List<MenuItemModel> MenuList { get; set; }
}

public class MenuInfo
{
    public int MenuId { get; set; }
    public string MenuName { get; set; }
    public int ParentId { get; set; }
    public string MKey { get; set; }
}

以上是关于2021-08-23 WPF控件专题 TreeView控件详解的主要内容,如果未能解决你的问题,请参考以下文章

2021-08-23 WPF控件专题 ContextMenu 控件详解

2021-08-23 WPF控件专题 Menu控件详解

2021-08-13 WPF控件专题 ComboBox 控件详解

2021-08-19 WPF控件专题 TabControl 控件详解

2021-08-09 WPF控件专题 Button控件详解

2021-08-17 WPF控件专题 Groupbox 控件详解