WPF Datagrid - 当值为null时,如何避免二级分组

时间:2014-06-23 20:04:36

标签: c# wpf xaml datagrid grouping

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

DataTable - 转换为CollectionView是以下DataGrid的数据源。数据表有两列,"类别"和" SubCategory"基于哪个两级分组应该分别进行 - 主要和次要分组。然而,问题是" SubCategory"并非适用于所有情况,某些行可能只有"类别" - 在这种情况下,我根本不想显示第二个组级别标题 - 现在它显示为"扩展器"切换按钮。

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

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

0 个答案:

没有答案