C# WPF TreeView 等级显示条目

本文介绍如何在C# WPF应用中创建一个具有层级结构的TreeView,详细讲解了数据绑定和控件配置,帮助开发者实现清晰地展示多级条目。
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TreeView Name="trvMenu">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:Class}" ItemsSource="{Binding Students}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="D:\代码\WpfApp18 - 副本 - 副本\WpfApp1\1.jpg"  Width="30" Height="30"/>
                        <TextBlock Text="{Binding ClassName}" />
                        <TextBlock Text=" [" Foreground="Blue" />
                        <TextBlock Text="{Binding Teacher}" Foreground="Blue" />
                        <TextBlock Text="]" Foreground="Blue" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type local:Student}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="D:\代码\WpfApp18 - 副本 - 副本\WpfApp1\1.jpg"   Width="30" Height="30" />
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text=" (" Foreground="Green" />
                        <TextBlock Text="{Binding Age}" Foreground="Green" />
                        <TextBlock Text=" years)" Foreground="Green" />
                    </StackPanel>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation;
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 WpfApp1
{
    public class BaseINotify : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void Notify(string str)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(str));
        }

    }

    public class Student : DependencyObject
    {


        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(Student), new PropertyMetadata(""));



        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set => SetValue(AgeProperty, value);
        }

        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeProperty =
            DependencyProperty.Register("Age", typeof(int), typeof(Student), new PropertyMetadata(0));



        public Student(string name,int age)
        {

            this.Age = age;
            this.Name = name;
        }

    }


    public class Class : BaseINotify
    {
        private string classname;
        private string teachter;
        private ObservableCollection<Student> students;

        public string ClassName
        {
            get => classname;
            set
            {
                if (classname == value) return;
                classname = value;
                Notify("ClassName");
            }
        }
        public string Teacher
        {
            get => teachter;
            set
            {
                if (teachter == value) return;
                teachter = value;
                Notify("Teacher");

            }
        }


        public ObservableCollection<Student> Students
        {
            get => students;
            set
            {
                if (students == value) return;
                students = value;
                Notify("Students");
            }
        }


        public Class(string cn, string tc, ObservableCollection<Student> ls)
        {
            this.ClassName = cn;
            this.Teacher = tc;
            this.Students = ls;
        }

    }
    public partial class MainWindow : Window
    {

        ObservableCollection<Class> studentGroups = new ObservableCollection<Class>();
        public MainWindow()
        {
            InitializeComponent();


            this.DataContext = studentGroups;

            studentGroups.Add(new Class("三年级一班", "小张老师", new ObservableCollection<Student>() { new Student("小王同学",15) }));

            trvMenu.ItemsSource = studentGroups;
        }
    }

    public class MenuItem
    {
        public MenuItem()
        {
            this.Items = new ObservableCollection<MenuItem>();
        }

        public string Title { get; set; }

        public ObservableCollection<MenuItem> Items { get; set; }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值