使用CollectionViewSource在数据网格上分组的问题

时间:2019-06-16 01:14:14

标签: c# wpf mvvm datagrid grouping

一段时间以来,我一直在尝试将数据网格中的数据分组,但是没有运气。在我的ViewModel中,属性:

public ObservableCollection<CompetitorModel> Competitors { get; } = 
            new ObservableCollection<CompetitorModel>();

public CollectionViewSource CompetitorsCollectionView { get; } =
            new CollectionViewSource();

在我的构造函数中:

CompetitorsCollectionView.Source = Competitors;

CompetitorsCollectionView.GroupDescriptions.Add(
                new PropertyGroupDescription(nameof(CompetitorModel.Qualification)));

最后,我在xaml中的数据网格:

<Grid Background="#FFE5E5E5">
    <!-- Datagrid Competition -->
    <DataGrid x:Name="Datagrid_Competition"
            ItemsSource="{Binding CompetitorsCollectionView}"
            AutoGenerateColumns="False"
            Grid.Column="2"
            Grid.Row="3"
            HorizontalGridLinesBrush="DarkGray"
            VerticalGridLinesBrush="DarkGray"
            IsReadOnly="False"
            CanUserReorderColumns="False"
            CanUserResizeRows="False"
            CanUserResizeColumns="True"
            CanUserDeleteRows="True"
            CanUserAddRows="False">
        <!-- Grouping Style -->
        <DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <!-- Column Information -->
        <DataGrid.Columns>
            <DataGridTextColumn Header="Place"
                    Binding="{Binding Path=Place}"
                    Width="45"
                    IsReadOnly="True"/>
            <DataGridTextColumn Header="Username"
                    Binding="{Binding Path=Username}"
                    Width="120"/>
            <DataGridTextColumn Header="VIStart"
                    Binding="{Binding Path=VIStart}"
                    Width="Auto"/>
            <DataGridTextColumn Header="VIEnd"
                    Binding="{Binding Path=VIEnd}"
                    Width="Auto"/>
            <DataGridTextColumn Header="VIs"
                    Binding="{Binding Path=VIs}"
                    Width="Auto"
                    IsReadOnly="True"/>
            <DataGridTextColumn Header="Time"
                    Binding="{Binding Path=TimeFormated}"
                    Width="Auto"
                    IsReadOnly="True"/>
            <DataGridTextColumn Header="Rerecords"
                    Binding="{Binding Path=Rerecords}"
                    Width="Auto"/>
            <DataGridCheckBoxColumn Header="DQ"
                    Binding="{Binding Path=DQ}"
                    Width="30"/>
            <DataGridTextColumn Header="DQReason"
                    Binding="{Binding Path=DQReasons}"
                    Width="Auto"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我使用ListCollectionView尝试了多种方法,这些方法确实有效,但是无法在将来进行分类或提供有用的功能。话虽如此,这是我的工具看起来像是用ListCollectionView而不是CollectionViewSource的样子: enter image description here

如果有人能指出我出了问题的地方,我将非常感激,因为我一生无法解决这个问题。目前,我找不到的其他帖子可以解决我的问题。

编辑:我不知道这对解决问题是否重要,但是在我的App.xaml中,我为数据网格分组定义了以下样式:

<!-- Datagrid Grouping Style -->
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander x:Name="expander"
                            IsExpanded="True"
                            Background="DarkGray"
                            Foreground="Black"
                            BorderBrush="DarkGray"
                            BorderThickness="1.75"
                            Margin="0,5">
                    <Expander.Header>
                        <TextBlock Text="{Binding Name}"/>
                    </Expander.Header>
                    <ItemsPresenter/>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:3)

这就是我的工作方式,但是我想知道是否还有另一种方法。

我创建了CompetitorCollection的{​​{1}}属性,并将其绑定到ICollectionView

DataGrid ItemsSource

在构造函数中

private ICollectionView _competitors;

public ICollectionView CompetitorCollection
 {
  get { return _competitors; }
 }

绑定 _competitors = CollectionViewSource.GetDefaultView(Competitors); _competitors.GroupDescriptions.Add(new PropertyGroupDescription(nameof(CompetitorModel.Qualification))); 作为CompetitorCollection项目来源。

DataGrid

替代方法

<DataGrid x:Name="Datagrid_Competition" ItemsSource="{Binding CompetitorCollection}" AutoGenerateColumns="False" Grid.Column="2" Grid.Row="3" HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" IsReadOnly="False" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserResizeColumns="True" CanUserDeleteRows="True" CanUserAddRows="False"> 内创建CollectionViewSource

Windows.Resource

然后在<Window.Resources> <CollectionViewSource x:Key="DQ" Source="{Binding Path=Competitors}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Qualification"/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> </Window.Resources> 中添加我们刚创建的DataGrid作为CollectionView

ItemSource

然后必须在ViewModel中仅存在<DataGrid x:Name="Datagrid_Competition" ItemsSource="{Binding Source={StaticResource DQ}}" AutoGenerateColumns="False" Grid.Column="2" Grid.Row="3" HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" IsReadOnly="False" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserResizeColumns="True" CanUserDeleteRows="True" CanUserAddRows="False"> (类型为Competitors)。