我有一个支持两级分组的网格,分组基于多个列 . 如果值为“null”,我该如何避免第二级分组 .

DataTable - 转换为CollectionView是以下DataGrid的数据源 . 数据表有多个两列,“Category”和“SubCategory”,基于这两列,应分别进行两级分组 - 主要和次要 . 但是,问题是“SubCategory”并不适用于所有情况,某些行可能只有“Category” - 在这种情况下,我不希望第二个组级别 Headers 显示 - 现在它使用“扩展器”切换按钮显示为空行 .

数据是灵活的,如果我必须对数据表进行更改 - 我可以这样做 . 请建议 .

<DataGrid Name="gridResult" IsSynchronizedWithCurrentItem="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
            ScrollViewer.CanContentScroll="True" AlternationCount="2"  ItemsSource="{Binding }" HeadersVisibility="All" CanUserAddRows="False" 
            CanUserDeleteRows="False" EnableRowVirtualization="False" EnableColumnVirtualization="False" Margin="2,10,2,2" BorderThickness="0" HorizontalGridLinesBrush="Gray"
            VerticalGridLinesBrush="Gray" Background="Transparent" AllowDrop="False" Grid.Row="2">
    <DataGrid.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <DataGridRowsPresenter/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource SecondGroupContainerStyle}">
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <DataGridRowsPresenter/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>


<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="#FFBDC8C9" Foreground="Black" FontWeight="Bold" BorderBrush="Black" BorderThickness="0 0 0 1">
                <Expander.Header>
                        <TextBlock Text="{Binding Name}"/>
                    <!--</StackPanel>-->
                </Expander.Header>
                <ItemsPresenter />
            </Expander>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

<Style x:Key="SecondGroupContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type GroupItem}" >
            <Expander x:Name="exp" IsExpanded="True" Background="#FFC2CBCC" Foreground="Black" FontWeight="Bold" BorderBrush="Black" BorderThickness="0 0 0 1" Margin="5 0 0 0">
                <Expander.Header>
                    <TextBlock Text="{Binding Name}">
                    </TextBlock>
                </Expander.Header>
                <ItemsPresenter />
            </Expander>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>