Powerbasic Museum 2020-B

IT-Consultant: Patrice Terrier => C# Programming => Topic started by: Steve Rossell on January 07, 2013, 01:06:47 AM

Title: WPF Datagrid: How do you suppress group header when it is null?
Post by: Steve Rossell on January 07, 2013, 01:06:47 AM
Hi,

I have a WPF 4 Datagrid that is bound to an ListView/ObservableCollection, that has a grouping on the column "Group". Sometimes the "Group" column will be null and in those instances I would like to suppress the displaying of the Group Header or make the Group Header Height zero. How can this be done? I have looked at GroupStyleSelector and Style Triggers, but am not sure this is the way to go or even how to use these.

Here is my XAML for the DataGrid and Group Style:

        <DataGrid x:Name="dgIngredients" Margin="0,3.4,0,0" Grid.Row="2" Background="transparent" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" ColumnWidth="*">

            <DataGrid.GroupStyle>
                <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel>
                                        <TextBlock FontWeight="Bold" Foreground="Blue" FontSize="24" Text="{Binding Name}" FontFamily="Segoe Script" Margin="40,0,0,0" MaxHeight="40"/>
                                        <ItemsPresenter/>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
            </DataGrid.GroupStyle>
           
        </DataGrid>


Thanks,

Steve Rossell
Title: Re: WPF Datagrid: How do you suppress group header when it is null?
Post by: Steve Rossell on January 07, 2013, 05:05:04 PM
I figured it out. I added a DataTrigger to the TextBlock to Collapse/Hide it when the value is null.

<TextBlock FontWeight="Bold" Foreground="Blue" FontSize="24" Text="{Binding Name}" FontFamily="Segoe Script" Margin="40,0,0,0" MaxHeight="40" Height="Auto">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>


Steve