WPF:ListView 自定义滚动查看器导致列标题消失 [英] WPF: ListView custom scrollviewer causes Column Header to disappear
问题描述
我一直在为我的 ListView 编写自定义 ScrollViewer 样式.应用样式(见下面的代码)后,它会从视图中删除列标题,只留下列表中的项目.
I am have been writing a custom ScrollViewer style for my ListView. Upon applying the style (see code below) it removes the Column Header from the view, leaving only the items in the list.
我似乎只能:
- 没有自定义滚动查看器的工作标题
- 没有标题的工作自定义滚动查看器
我无法弄清楚为什么会发生这种情况.我应该怎么做才能让它发挥作用?
I cannot work out why this is happening. What I should do to get this to work?
我相信这是因为我在我的风格中定义了一个空的 ItemsPresenter
(如下)但这是其他人似乎正在做/推荐的.
I believe this is due to the fact that I am defining an empty ItemsPresenter
in my style (below) but this is what other people seem to be doing/recommending.
列表视图样式
<Style x:Key="StandardListView" TargetType="{x:Type ListView}">
<Setter Property="Background" Value="{DynamicResource TransparentWhite}" />
<Setter Property="Foreground" Value="{DynamicResource TextParagraphLightGreyP1}" />
<Setter Property="BorderBrush" Value="{DynamicResource ControlOutlineDisabled}" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<ScrollViewer Style="{DynamicResource StandardScrollViewer}">
<ItemsPresenter >
</ItemsPresenter>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
自定义滚动查看器样式
<Style x:Key="StandardScrollViewer" TargetType="{x:Type ScrollViewer}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="{DynamicResource BackgroundGreyLevel2}" />
<Setter Property="Background" Value="{DynamicResource CollectionControlBackgroundGradient}" />
<Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollContentPresenter Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<ScrollBar Name="PART_VerticalScrollBar"
HorizontalAlignment="Right"
Opacity="0.5"
Grid.Column="1"
Value="{TemplateBinding VerticalOffset}"
Maximum="{TemplateBinding ScrollableHeight}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
<ScrollBar Name="PART_HorizontalScrollBar"
VerticalAlignment="Bottom"
Orientation="Horizontal"
Opacity="0.5"
Grid.Row="1"
Value="{TemplateBinding HorizontalOffset}"
Maximum="{TemplateBinding ScrollableWidth}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
视图中的列表
<ListView x:Name="Licences" Margin="2,20,2,25" Style="{DynamicResource StandardListView}">
...
推荐答案
GridView
标题是 ScrollViewer
的一部分.这个模板应该可以工作:
The GridView
headers are part of the ScrollViewer
. This template should work:
<ControlTemplate TargetType="ListView">
<ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter >
</ItemsPresenter>
</ScrollViewer>
</ControlTemplate>
如果您想要一个带有标题的自定义 ScrollViewer
,您需要创建一个.您可以从下面的默认 GridView.GridViewScrollViewerStyleKey
样式开始,并根据您的要求进行修改:
If you want a custom ScrollViewer
with headers, you need to create one. You can start from the default GridView.GridViewScrollViewerStyleKey
style below and modify it as per your requirements:
<Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}">
<Setter Property="UIElement.Focusable" Value="false"/>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid Background="{TemplateBinding Control.Background}" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel Margin="{TemplateBinding Control.Padding}">
<ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
Focusable="false">
<GridViewHeaderRowPresenter Margin="2,0,2,0" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderStringFormat="{Binding Path=TemplatedParent.View.ColumnHeaderStringFormat, RelativeSource={RelativeSource TemplatedParent}}"
AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}">
<GridViewRowPresenterBase.Columns>
<Binding Path="TemplatedParent.View.Columns" RelativeSource="{RelativeSource TemplatedParent}"/>
</GridViewRowPresenterBase.Columns>
</GridViewHeaderRowPresenter>
</ScrollViewer>
<ScrollContentPresenter Name="PART_ScrollContentPresenter" KeyboardNavigation.DirectionalNavigation="Local"
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
</DockPanel>
<ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1" Minimum="0.0"
Maximum="{TemplateBinding ScrollViewer.ScrollableWidth}"
ViewportSize="{TemplateBinding ScrollViewer.ViewportWidth}"
Visibility="{TemplateBinding ScrollViewer.ComputedHorizontalScrollBarVisibility}" Cursor="Arrow"
Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/>
<ScrollBar Name="PART_VerticalScrollBar" Orientation="Vertical" Grid.Column="1" Minimum="0.0"
Maximum="{TemplateBinding ScrollViewer.ScrollableHeight}"
ViewportSize="{TemplateBinding ScrollViewer.ViewportHeight}"
Visibility="{TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility}" Cursor="Arrow"
Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/>
<DockPanel Grid.Column="1" Grid.Row="1" LastChildFill="false"
Background="{Binding Path=Background, ElementName=PART_VerticalScrollBar}">
<Rectangle DockPanel.Dock="Left" Width="1" Fill="White"
Visibility="{TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility}"/>
<Rectangle DockPanel.Dock="Top" Height="1" Fill="White"
Visibility="{TemplateBinding ScrollViewer.ComputedHorizontalScrollBarVisibility}"/>
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这篇关于WPF:ListView 自定义滚动查看器导致列标题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!