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>
相关推荐
Macbethad3 小时前
基于WPF的Ethernet/IP主站程序技术方案
网络协议·tcp/ip·wpf
张人玉6 小时前
Prism Template Pack 完整使用示例(VS2022 + .NET 8 + DryIoc)
.net·wpf·prism
棉晗榜7 小时前
wpf 在XAML中配置视图模型,通过 d:DataContext设置设计时类型,方便按F12跳转查看类型
wpf
赵财猫._.10 小时前
HarmonyOS渲染性能优化:组件树复用与局部刷新机制
wpf·harmonyos·ux
赵财猫._.10 小时前
鸿蒙分布式数据库同步:冲突解决与数据一致性策略
wpf·harmonyos·ux
Macbethad20 小时前
使用WPF编写一个数据记录页面
wpf
dotent·3 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图4 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞4 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii4 天前
WPF入门指南:解析默认项目结构
wpf