How to show grouping in WPF Data Grid with several levels?

I'm relatively new to WPF, so I understand about styles and setters, but I am having problems with this.

I am using WPF Data Grid and should show several levels of grouping. I would like the levels of the 2nd and 3rd groups to be more indented than the upper level.

The following code will show the levels of the group, but it shows them one on the right one above the other and makes it impossible to determine the levels of nested groups.

<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>

code>

How can I get the group title to indent based on level?

+3
source share
5 answers

, - . , , .

CollectionViewSource:

<Window.Resources>
    <CollectionViewSource x:Key="cvs" Source="{Binding TestData}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="TopProperty"/>
            <PropertyGroupDescription PropertyName="SubProperty"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

DataGrid XAML 2 GroupStyles, - . StackPanel , , , .

<DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel>
                                        <Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,0,0,1" Margin="5,0,0,0">                                            
                                            <StackPanel   Height="23" Orientation="Horizontal" Margin="3,0,0,0" Background="#FFE6E6E6">
                                                <TextBlock FontWeight="Bold"  Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </Border>
                                            <ItemsPresenter />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Border Background="#FF959595" BorderBrush="#FF727272" BorderThickness="0,0,0,1" Margin="5,0,0,0">
                                <StackPanel   Height="23" Orientation="Horizontal" Margin="3,0,0,0" Background="#FFF3F3F3">
                                    <TextBlock FontWeight="Bold"  Text="{Binding Path=Name}" Margin="55,0,0,0" Width="100" VerticalAlignment="Center"/>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
</DataGrid.GroupStyle>

: https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx

+3

, . ItemsPresenter, , . , . ...

+2

( , ), WPF Datagrid .

this.

Xceed DataGrid . , , .

+1

: Multi Grouping DataGrid , , "PagedCollectionView" - Silverlight.

+1

I already use ICollectionView and PropertyGroupDescription to add grouping, however you should still use XAML to describe how the grouping will be displayed, otherwise you will not get any indication of the interface for grouping.

I am wondering how to configure XAML so that I can represent several levels of grouping, preferably with some kind of indentation to the group level or something like that ....

0
source

Source: https://habr.com/ru/post/1744559/


All Articles