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>
相关推荐
He BianGu1 小时前
示例:WPF中推荐一个Diagram开源流程图控件
wpf·流程图·diagram
小海聊工控上位机3 小时前
WPF引入控件模板
c#·wpf
mrchip15 小时前
Simple WPF: WPF 透明窗体和鼠标事件穿透
c#·wpf
网虫132518 小时前
WPF----自定义滚动条ScrollViewer
wpf
mingupup2 天前
WPF/C#:BusinessLayerValidation
c#·wpf
云草桑2 天前
WPF UI交互专题 界面结构化处理 查看分析工具Snoopy 逻辑树与视觉树 平面图像 平面图形 几何图形 弧线 01
ui·c#·asp.net·wpf·wpfui
ggtc3 天前
WPF网格类型像素着色器
wpf·shader
搬砖的工人3 天前
了解WPF控件:OpenFileDialog常用属性与用法(十六)
wpf
海盗12343 天前
C# 唯一性进程的方法封装(Winform/WPF通用)
java·c#·wpf
云草桑3 天前
WPF 2024 金九银十 最新 高级 架构 面试题 C#
面试·性能优化·c#·wpf·prism