2

我在我的 ListView 上应用了以下样式。但是这种样式导致我的 GridView 隐藏其标题行。如果我从 ListView 中删除此样式。可以看到标题行。我应该如何调整这种样式以显示标题行?

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

下面是我的 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>

还有我的 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>
4

2 回答 2

1

问题在于您的 ScrollViewer 包裹了 ItemsPresenter。

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

您还没有发布模板TreeViewScrollViewerControlTemplate。我猜你GridViewHeaderRowPresenter在那边不见了。

无论如何,您可以使用提供的默认样式,即GridView.GridViewScrollViewerStyleKey.

<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"
      Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
  <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
于 2014-02-20T17:11:10.917 回答
1

如果您查看示例 ListView 模板,您可以看到标题是使用GridViewHeaderRowPresenter呈现的

您需要在样式的控件模板中添加一个。

于 2014-02-20T16:12:44.280 回答