WPF 数据分组显示

WPF 数据分组显示

效果展示:

Student类:

csharp 复制代码
public class Student
{
    public string Name { get; set; }

    public string Class { get; set; }

    public int Age { get; set; }
}

MainWindow.xaml.cs

csharp 复制代码
public partial class MainWindow : Window
{

    private ObservableCollection<Student> students;

    public IEnumerable<IGrouping<string,Student>> GroupeStudent { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        students = new ObservableCollection<Student>()
        {
            new Student(){ Name = "赵一", Class = "初中", Age = 14},
            new Student(){ Name = "钱二", Class = "小学", Age = 9},
            new Student(){ Name = "孙三", Class = "高中", Age = 16},
            new Student(){ Name = "李四", Class = "初中", Age = 15},
            new Student(){ Name = "周五", Class = "高中", Age = 17},
            new Student(){ Name = "吴六", Class = "高中", Age = 16},
            new Student(){ Name = "郑七", Class = "小学", Age = 8},
            new Student(){ Name = "王八", Class = "初中", Age = 14}
        };

        GroupeStudent = students.GroupBy(x => x.Class);

        this.DataContext = this;
    }
}

MainWindow.xaml

csharp 复制代码
<Grid Margin="40">
    <ListView ItemsSource="{Binding GroupeStudent}">
        <ListView.ItemTemplate>
            <DataTemplate >
                <Expander Header="{Binding Key}">
                    <ListView ItemsSource="{Binding}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Name}" Margin="10"/>
                                    <TextBlock Text="{Binding Class}" Margin="10"/>
                                    <TextBlock Text="{Binding Age}" Margin="10"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ListView>
                </Expander>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
相关推荐
500848 小时前
昇腾 CANN 的五层架构,到底分了哪五层
java·人工智能·分布式·架构·ocr·wpf
醉颜凉14 小时前
ZooKeeper Zxid 与 Epoch 深度解析:分布式事务的时空坐标
分布式·zookeeper·wpf
5008414 小时前
HCCL 集合通信编程:多卡协同的正确姿势
java·flutter·性能优化·electron·wpf
5008415 小时前
用 Ascend CL 从零写一个推理程序
人工智能·深度学习·机器学习·性能优化·wpf
彦为君18 小时前
Spring定时任务开发指南(动态实现)
java·开发语言·后端·python·spring·wpf
她说彩礼65万19 小时前
WPF中Style和ControlTemplate的触发器有什么不同
wpf
玖笙&1 天前
✨WPF编程基础【3.3】:容器控件(附源码)
c++·wpf·visual studio
500842 天前
GE 怎么做算子融合
分布式·架构·开源·wpf
500842 天前
Conv + BN + ReLU 融合:省掉两次显存读写
flutter·架构·开源·wpf·音视频
500842 天前
把 FlashAttention 讲清楚
flutter·electron·wpf