Kerfluffel Kerfluffel - 1 month ago 26
C# Question

WPF ListView GroupStyle GridViewHeaderRowPresenter doesn't hide original headers

enter image description here

I have a listview that uses groupstyles where the Column headers are shown in each group. This works fine. I would however like to hide the original ListView Column Header (seen at the top of image) and just show Headers within the groups. I have tried everything I can think of but one affects the other and can not figure out how to separate them so it only show in the groups.

The GroupStyle:

<Style x:Key="GroupingSerialStyle" TargetType="{x:Type GroupItem}" BasedOn="{StaticResource GroupingBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsExpanded="{Binding IsExpandedAll, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=OneTime}" Margin="2">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text=" # "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</Expander.Header>
<StackPanel Orientation="Vertical">
<GridViewHeaderRowPresenter Visibility="Visible" Margin="15,0,0,0"
DataContext="{Binding View, RelativeSource={RelativeSource FindAncestor,ListView,1}}"
Columns="{Binding Columns}"
ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyleABCD}"
ColumnHeaderTemplate="{Binding ColumnHeaderTemplate}"
ColumnHeaderTemplateSelector="{Binding ColumnHeaderTemplateSelector}"
AllowsColumnReorder="{Binding AllowsColumnReorder}"
ColumnHeaderContextMenu="{Binding ColumnHeaderContextMenu}"
ColumnHeaderToolTip="{Binding ColumnHeaderToolTip}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<ItemsPresenter Margin="15,0,0,0" />
</StackPanel>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Answer

It can be solved through the ColumnHeaderContainerStyle. If you set an empty Template at GridView.ColumnHeaderContainerStyle and use your styled GridViewColumnHeaderStyleABCD for your GroupItem it should work.

I tried the following, that only displays column headers at the group items.

<ListView BindingGroup="{Binding Name}" ItemsSource="{Binding Source={StaticResource TestItems}}">
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                                <!-- Empty: ListViews header shows nothing -->
                            </ControlTemplate>
                        </Setter.Value>
                        </Setter>
                    </Style>
                </GridView.ColumnHeaderContainerStyle>
                            <GridViewColumn DisplayMemberBinding="{Binding Parameter0}" Header="Par0" />
            <GridViewColumn DisplayMemberBinding="{Binding Parameter1}" Header="Par1" />
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <Expander Margin="2">
                        <Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text=" # " />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </Expander.Header>
                        <StackPanel Orientation="Vertical">
                            <GridViewHeaderRowPresenter Margin="15,0,0,0"
                                                    Columns="{Binding Columns}"
                                                    DataContext="{Binding View, RelativeSource={RelativeSource FindAncestor, ListView, 1}}"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                    Visibility="Visible" />
                            <ItemsPresenter Margin="15,0,0,0" />
                        </StackPanel>
                    </Expander>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>