ListView GridViewColumn可见性因自定义样式而影响

时间:2014-02-20 16:04:51

标签: wpf gridview gridviewcolumn

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

2 个答案:

答案 0 :(得分:1)

如果您查看example ListView template,可以看到标题是使用GridViewHeaderRowPresenter

呈现的

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

答案 1 :(得分: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>