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>
相关推荐
影寂ldy20 小时前
WPF导航窗体+Page页面跳转 知识点整理
wpf
He BianGu20 小时前
【WPF-Control教程】IOC 依赖注入
c#·wpf·wpf-control
He BianGu20 小时前
【WPF-Control教程】H.Extensions.Behvaiors 开发文档
c#·wpf·wpf-control
He BianGu20 小时前
【WPF-Control教程】H.Style 开发文档
c#·wpf·wpf-control
麻瓜code21 小时前
分布式锁演进:从一个用户下两单,到 Redisson,我改了四版
分布式·wpf
He BianGu21 小时前
【WPF-Control教程】MVVM 二次开发文档
c#·wpf·wpf-control
He BianGu21 小时前
【WPF-Control教程】Theme 主题系统二次开发文档
c#·wpf·wpf-control
小羊没烦恼!4 天前
分布式事务 | 使用DTM 的Saga 模式
java·linux·运维·数据库·wpf
samble5 天前
从零搭建半导体设备上位机(二):Prism + DryIoc,五个工程与区域导航怎么串起来
wpf·prism·dryioc
一条闲鱼_mytube5 天前
分布式 ID 生成方案:雪花算法与号段模式
分布式·算法·wpf