Question

I have applied the following style on my ListView. But this Style is causing my GridView to hide its headers row. If I remove this style from the ListView. The headers row is seen. How should I tweak this style to display the headers row?

<Style x:Key="listViewStyle" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <!--<Border x:Name="Bd" BorderBrush="White" Opacity="0.1" BorderThickness="2" Background="{TemplateBinding Background}" Padding="1.5" SnapsToDevicePixels="true">-->
                <Grid>
                    <Border x:Name="Bd" BorderBrush="White" Opacity="0.1" Background="{TemplateBinding Background}" Padding="1.5" SnapsToDevicePixels="true" />
                    <!--<Border CornerRadius="5" Background="White" Opacity="0.1" />-->
                    <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}" Template="{DynamicResource TreeViewScrollViewerControlTemplate}" >
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Grid>
                <!--</Border>-->
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Below is my ListView XAML:

<ListView ItemsSource="{Binding PersonList}" Width="750" MaxWidth="750" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" Height="200"
              HorizontalAlignment="Right" Margin="20" util:GridViewSort.AutoSort="True">
        <ListView.Style>
            <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource listViewStyle}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=gridViewRB, Path=IsChecked}" Value="true">
                        <Setter Property="View" Value="{StaticResource ResourceKey=GridView}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>
    </ListView>

And my GridView:

<GridView x:Key="GridView">
    <GridViewColumn>
        <GridViewColumn.HeaderTemplate>
            <DataTemplate>
                <CheckBox Name="cbSelectAll" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.SelectAllCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </DataTemplate>
        </GridViewColumn.HeaderTemplate>
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>
    <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" util:GridViewSort.PropertyName="WeightInGrams" />
    <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
    <GridViewColumn Header="Ip Address" DisplayMemberBinding="{Binding Path=IpAddress}" />
</GridView>
Was it helpful?

Solution

Issue is in your ScrollViewer wrapping over ItemsPresenter.

<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"
              Template="{DynamicResource TreeViewScrollViewerControlTemplate}" >
   <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>

You haven't posted template TreeViewScrollViewerControlTemplate. I guess you are missing GridViewHeaderRowPresenter over there.

Anyhow you can use default style provided i.e. GridView.GridViewScrollViewerStyleKey.

<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"
      Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
  <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>

OTHER TIPS

If you look at the example ListView template you can see that the headers are rendered using a GridViewHeaderRowPresenter

You need to add one to your style's control template.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top