<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); }
}
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);
}
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; }
}
}