当前位置:   article > 正文

WPF的DataGrid控件——分组、改变成DataTreeGrid的样式_wpf datagrid 分组

wpf datagrid 分组

这里主要讲到《WPF编程宝典》那本书中,关于DataGrid控件中一些补充内容。
1、DataGrid基本用法
直接上代码,看效果吧:
后台代码

namespace DataGridTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Model model = new Model();
            this.dataGridStudent.ItemsSource = model.ListStudent;
        }

    }
    public class Model
    {
        public List<Student> ListStudent { get; set; }
        public Model()
        {
            ListStudent = Initial();
        }
        private List<Student> Initial()
        {
            List<Student> listStudent = new List<Student>();
            listStudent.Add(new Student("class1", "David", 101));
            listStudent.Add(new Student("class1","James",102));
            listStudent.Add(new Student("class1", "Jim", 103));
            listStudent.Add(new Student("class2", "Lilei", 201));
            listStudent.Add(new Student("class2", "Hanmeimei", 202));
            listStudent.Add(new Student("class2", "Zhaolei", 203));
            return listStudent;
        }
    }
    public class Student
    {
        public string ClassRoom { get; set; }
        public string Name { get; set; }
        public int StudentId { get; set; }
        public Student(string classRoom,string name,int studentId)
        {
            ClassRoom = classRoom;
            Name = name;
            StudentId = studentId;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

xaml代码:

    <Grid>
        <DataGrid Name="dataGridStudent" DataContext="{Binding Path=ListStudent}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ClassRoom" Binding="{Binding Path=ClassRoom}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="StudentId" Binding="{Binding Path=StudentId}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里写图片描述
2、分组
在上面的数据中,可以以ClassRoom来给数据分组
后台代码(主要修改MainWindow()即可):

public MainWindow()
        {
            InitializeComponent();
            ObservableCollection<Student> data = new ObservableCollection<Student>();
            Model model = new Model();
            foreach(var item in model.ListStudent)
            {
                data.Add(item);
            }
            this.dataGridStudent.ItemsSource = data;
            ICollectionView vw = CollectionViewSource.GetDefaultView(data);
            vw.GroupDescriptions.Add(new PropertyGroupDescription("ClassRoom"));
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

前台代码:

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <DataGrid Name="dataGridStudent"  AutoGenerateColumns="False">
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ClassRoom" Binding="{Binding Path=ClassRoom}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="StudentId" Binding="{Binding Path=StudentId}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

这里写图片描述
3、样式修改
这个时候会发现界面样式比较丑,可以稍作修改,可以参考以下博客:
http://blog.csdn.net/ff1990723/article/details/52948467
这篇博客稍复杂,作为入门没多久的半小白,我是看了半天没怎么看明白。但是我工作的需求并没有那么高,只需要修改Expander的按钮,把头列去掉即可,然后改为TreeView的样式。
首先去掉头列,只需要简单的设置RowHeaderWidth=”0”:

<DataGrid Name="dataGridStudent"  AutoGenerateColumns="False" RowHeaderWidth="0">
  • 1

然后修改Expander的按钮:
这里的expander有两部分组成,一个是togglebutton,一部分是内容,这里可以用一个有两行的Grid的容器来存放这两个内容。当IsExpanded为True的时候,grid.row=1的行高等于内容的行高,当IsExpanded为false的时候,grid.row=1的行高为0,这样就可以变成expander的收放状态。这里仅需修改前台代码即可。

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">
            <Border>
                <Path Name="path" Data="M 5,4 L 5,11 L 11,7.5 Z" Fill="Black"></Path>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter TargetName="path" Property="Data" Value="M 4,5 L 11,5 L 7.5,11 Z"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

            <Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
            <Setter Property="IsExpanded" Value="False"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition></RowDefinition>
                                <RowDefinition Name="ContentRow" Height="0"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" Name="border" BorderThickness="0,0,0,0">
                                <WrapPanel>
                                    <ToggleButton Margin="20 10 0 10"  VerticalAlignment="Center" IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                                  OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" />
                                    <ContentPresenter  Margin="20 10 0 10"  VerticalAlignment="Center"  ContentSource="Header"  RecognizesAccessKey="True" />
                                </WrapPanel>
                            </Border>
                            <ContentPresenter Grid.Row="1"></ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content, Path=DesiredHeight}" />
                            </Trigger>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter TargetName="border" Property="BorderThickness" Value="0 0 0 1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="False" Style="{StaticResource ExpanderStyle}">
                            <Expander.Header>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Expander.Header>
                            <Expander.Content>
                                <ItemsPresenter />
                            </Expander.Content>                            
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid>        
        <DataGrid Name="dataGridStudent"  AutoGenerateColumns="False" RowHeaderWidth="0">
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ClassRoom" Binding="{Binding Path=ClassRoom}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="*"></DataGridTextColumn>
                <DataGridTextColumn Header="StudentId" Binding="{Binding Path=StudentId}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

这里写图片描述

接下来,把网格去掉,只需要设置网格线为空即可。

<DataGrid Name="dataGridStudent"  AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}">
  • 1

然后,这个时候已经离TreeView很近了,我们只需要把第一列去掉,然后把背景颜色也去掉,就是一个DataGrid和TreeView结合的控件。
这里写图片描述

最后的xaml代码如下:

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">
            <Border>
                <Path Name="path" Data="M 5,4 L 5,11 L 11,7.5 Z" Fill="Black"></Path>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter TargetName="path" Property="Data" Value="M 4,5 L 11,5 L 7.5,11 Z"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

            <Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
            <Setter Property="IsExpanded" Value="False"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition></RowDefinition>
                                <RowDefinition Name="ContentRow" Height="0"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" Name="border" BorderThickness="0,0,0,0">
                                <WrapPanel>
                                    <ToggleButton Margin="20 10 0 10"  VerticalAlignment="Center" IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                                  OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" />
                                    <ContentPresenter  Margin="20 10 0 10"  VerticalAlignment="Center"  ContentSource="Header"  RecognizesAccessKey="True" />
                                </WrapPanel>
                            </Border>
                            <ContentPresenter Grid.Row="1"></ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content, Path=DesiredHeight}" />
                            </Trigger>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter TargetName="border" Property="BorderThickness" Value="0 0 0 1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="False" Style="{StaticResource ExpanderStyle}">
                            <Expander.Header>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Expander.Header>
                            <Expander.Content>
                                <ItemsPresenter />
                            </Expander.Content>                            
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid>        
        <DataGrid  Name="dataGridStudent"  AutoGenerateColumns="False" RowHeaderWidth="0" HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}" Background="{x:Null}">
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" HorizontalAlignment="Center"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn Header="StudentId" Binding="{Binding Path=StudentId}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/582886
推荐阅读