码迷,mamicode.com
首页 > Windows程序 > 详细

wpf-DataGrid 分组

时间:2020-05-16 12:26:04      阅读:80      评论:0      收藏:0      [点我收藏+]

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

C# 数据:

namespace _1.DataGridStudy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
     ObservableCollection<Student> studentList;
     ICollectionView vm;
        public MainWindow()
        {
            InitializeComponent();
            studentList = new ObservableCollection<Student>();
            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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!