Home > database >  WPF conditional datagrid grouping
WPF conditional datagrid grouping

Time:11-03

I have a DataGrid with grouping based on customer name, and it works.

    <GroupBox Header="{Binding ElementName=MainWindow, Path=ocS3FileListCount}" ContentStringFormat="" Name="grpRemote" Margin="5,0,0,0" Grid.Column="1" Grid.Row="2">
        <GroupBox.Resources>
            <CollectionViewSource x:Key="S3List" Source="{Binding ElementName=MainWindow, Path=ocS3FileList}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="CustomerName"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </GroupBox.Resources>
        <DataGrid x:Name="dgS3List" Margin="0,0,0,0" ItemsSource="{Binding Source={StaticResource S3List}}" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Name}" />
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="File Name" Width="*" Binding="{Binding Path=FileName}" IsReadOnly="True" />
                <DataGridTextColumn Header="Received" Width="100" Binding="{Binding Path=JobReceived,StringFormat=d}" IsReadOnly="True" Stylus.IsPressAndHoldEnabled="True" />
                <DataGridTextColumn Header="Date" Width="100" Binding="{Binding Path=JobDate,StringFormat=d}" IsReadOnly="True" Stylus.IsPressAndHoldEnabled="True" />
            </DataGrid.Columns>
        </DataGrid>
    </GroupBox>

I do have a few users that would like the option to turn grouping on and off for different uses. I've used conditional styles before based on the status of a different control, but is it possible to have a conditional GroupStyle?

Having it enabled based on a toolbar checkbox seems like a good solution:

<CheckBox x:Name="chkGroupSwitch" IsChecked="True">Enable Grouping</CheckBox>

CodePudding user response:

I don't know about a way to make your GroupStyle style conditional in XAML, but since the GroupStyle only applies if you have a GroupDescription in your ViewSource, I would use a different approach:

The solution in C# is simple. All you have to do is add/remove the ViewSource's PropertyGroupDescription whenever the state of your checkbox changes.

eg.

private void ToggleGroupingEnabled(bool mode)
{
    CollectionViewSource viewSource = grpRemote.Resources["MyViewSourceName"] as CollectionViewSource;
    viewSource.GroupDescriptions.Clear();

    if (mode)
    {
        var groupDesc = new PropertyGroupDescription("CustomerName");
        viewSource.GroupDescriptions.Add(groupDesc);
    }
}

Assuming your ViewSource has a name:

 <CollectionViewSource x:Key="S3List" x:Name="MyViewSourceName" Source="{Binding ElementName=MainWindow, Path=ocS3FileList}">
  • Related