Grouping data on Datagrid wpf

最后都变了- 提交于 2019-12-22 18:47:10

问题


I want to regroup my data with an expander that contains the group name and contains all ClassMate name inside.

This is my class Group :

public class Group{ 
public List<ClassMate> CLGroup { get; set; }
public string GroupName { get; set; }}

My classMate class :

public class ClassMate: INotifyPropertyChanged{
public string Name { get; set; }
public string DisplayName { get; set; }}

I tried to do this with this Xaml but I don't know why it's not working

<DataGrid x:Name="gridMates"  ItemsSource="{Binding Groups }"  >
    <DataGrid.GroupStyle>
        <!-- Style for groups at top level. -->
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="True" >
                                    <Expander.Header>
                                        <DockPanel>

                                        <TextBlock Text="{Binding Path=GroupName}" />
                                    </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsControl ItemsSource="{Binding Path=CLGroup}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <StackPanel>
                                                        <TextBlock   Text="{Binding Path=DisplayName}"/>

                                                    </StackPanel>

                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>

    </DataGrid.GroupStyle>

What am I doing wrong? Thanks


回答1:


You need to bind the ItemsSource property to a grouped source collection. The easiest way to do this is to use a CollectionViewSource:

<Grid>
    <Grid.Resources>
        <CollectionViewSource x:Key="groups" Source="{Binding Groups}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="GroupName" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Grid.Resources>
    <DataGrid x:Name="gridMates" ItemsSource="{Binding Source={StaticResource groups}}" AutoGenerateColumns="False">
        <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>
                                            <DockPanel>
                                                <TextBlock Text="{Binding Path=Name}" />
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsControl ItemsSource="{Binding Path=Items[0].CLGroup}">
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding Path=DisplayName}"/>
                                                        </StackPanel>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>
</Grid>



回答2:


XAML Design

<Window.Resources>
    <Style x:Key="groupheaderstyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander x:Name="exp" IsExpanded="True" Background="White" Foreground="Black">
                        <Expander.Header>
                            <TextBlock Text="{Binding Gropname}" />
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<DataGrid x:Name="dgdata" HorizontalAlignment="Left" Height="269" VerticalAlignment="Top" Width="292">
    <DataGrid.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource groupheaderstyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <DataGridRowsPresenter />
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

XAML Design.CS

public class group
    {
        public string groupname { get; set; }
        public string CLgroup { get; set; }
        public string displayname { get; set; }
    }
    public Window4()
    {
        InitializeComponent();
        ObservableCollection<group> samdata = new ObservableCollection<group>
        {
            new group{groupname="Group1",CLgroup="xxx",displayname="demo1"},
            new group{groupname="Group1",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group1",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group2",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group2",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group2",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group3",CLgroup="zzz",displayname="demo3"},
            new group{groupname="Group3",CLgroup="yyy",displayname="demo2"},
            new group{groupname="Group3",CLgroup="yyy",displayname="demo2"},
        };          
        ListCollectionView collection = new ListCollectionView(samdata);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("groupname"));
        dgdata.ItemsSource = collection;
    }


来源:https://stackoverflow.com/questions/42898450/grouping-data-on-datagrid-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!