【发布时间】:2018-10-09 10:37:47
【问题描述】:
我一直在为我的 ListView 编写自定义 ScrollViewer 样式。应用样式后(参见下面的代码),它会从视图中删除列标题,只留下列表中的项目。
我似乎只能:
- 没有自定义滚动查看器的工作标题
- 没有标题的自定义滚动查看器
我无法弄清楚为什么会这样。我应该怎么做才能让它工作?
编辑:
我相信这是因为我以我的风格定义了一个空的ItemsPresenter(如下),但这是其他人似乎正在做/推荐的事情。
列表视图样式
<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}">
...
【问题讨论】:
-
@Clemens 不幸的是,我尝试使用它无济于事
标签: c# wpf listview styles scrollviewer