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>
相关推荐
冷眼Σ(-᷅_-᷄๑)2 小时前
WPF缩放动画和平移动画叠加后会发生什么?
wpf·动画
△曉風殘月〆4 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
.net开发12 小时前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo12 小时前
WPF 中 NavigationWindow 与 Page 的继承关系解析
wpf
SongYuLong的博客13 小时前
C# WPF 打印机
wpf
就是有点傻13 小时前
WPF中的转换器
wpf
.net开发19 小时前
WPF使用prism框架发布订阅实现消息提示
c#·.net·wpf
那少年已不再......1 天前
C#WPF使用CommunityToolkit.Mvvm库
开发语言·c#·wpf
SEO-狼术2 天前
Syncfusion Essential Studio WPF 2024 Crack
wpf
Olivia_vivi2 天前
WPF XAML
ui·wpf