wpf-DataGrid 分组

2021-03-05 10:27

阅读:666

标签:ups   useradd   name   initial   user   tac   pre   ace   ini   

C# 数据:

namespace _1.DataGridStudy
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
     ObservableCollection studentList;
     ICollectionView vm;
        public MainWindow()
        {
            InitializeComponent();
            studentList = new ObservableCollection();
            studentList.Add(new Student() { 学号 = 1, 姓名 = "Alex", 班级 ="二班"});
            studentList.Add(new Student() { 学号 = 2, 姓名 = "Peter", 班级 = "一班" });
            studentList.Add(new Student() { 学号 = 3, 姓名 = "Zack", 班级 = "三班" });
            studentList.Add(new Student() { 学号 = 4, 姓名 = "TJ", 班级 = "二班" });
            studentList.Add(new Student() { 学号 = 5, 姓名 = "Jack", 班级 = "一班" });
            studentList.Add(new Student() { 学号 = 6, 姓名 = "Jacky", 班级 = "三班" });
            studentList.Add(new Student() { 学号 = 7, 姓名 = "James", 班级 = "一班" });

            dg1.ItemsSource = studentList;
            vm = CollectionViewSource.GetDefaultView(studentList);
            vm.GroupDescriptions.Add(new PropertyGroupDescription("班级"));
            //vm.SortDescriptions.Add(new SortDescription("学号",ListSortDirection.Descending));
        }


    }
    public class Student
    { 
        public int 学号 { get; set; }
        public string 姓名 { get; set; }
        public string 班级 { get; set; }
    }

xaml:

 

 Grid>

        DataGrid Name="dg1" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" CanUserAddRows="False" Margin="0">
                DataGrid.Columns>
                    DataGridTextColumn Header="学号" Binding="{Binding 学号}" />
                    DataGridTextColumn Header="姓名" Binding="{Binding 姓名}"  />
                    DataGridTextColumn Header="班级" Binding="{Binding 班级}"  />
                DataGrid.Columns>
                DataGrid.GroupStyle>
                    GroupStyle >
                        GroupStyle.ContainerStyle>
                            Style TargetType="{x:Type GroupItem}">

                                Setter Property="Template">
                                    Setter.Value>
                                        ControlTemplate TargetType="{x:Type GroupItem}">
                                            Expander IsExpanded="True">
                                                Expander.Header>
                                                    StackPanel Orientation="Horizontal">
                                                        TextBlock Text="{Binding Name}" />
                                                        TextBlock Text="    数量:"/>
                                                        TextBlock Text="{Binding ItemCount}"/>
                                                    StackPanel>
                                                Expander.Header>
                                                ItemsPresenter/>
                                            Expander>
                                        ControlTemplate>
                                    Setter.Value>
                                Setter>
                            Style>
                        GroupStyle.ContainerStyle>
                    GroupStyle>
                DataGrid.GroupStyle>
            DataGrid>
        
       
    Grid>

 

wpf-DataGrid 分组

标签:ups   useradd   name   initial   user   tac   pre   ace   ini   

原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12899574.html


评论


亲,登录后才可以留言!