我正在使用我们使用标准 ControlTemplates 创建的自定义滚动条,但是当我将它们应用到 ListBox 时,右下角有一个角,我无法找到任何方法来覆盖它。
不幸的是,在我获得更多积分之前,我无法发布图片。但是我指的角落是当垂直和水平滚动条都出现时,右下角有一个空间填充了我无法覆盖的灰白色
我正在使用我们使用标准 ControlTemplates 创建的自定义滚动条,但是当我将它们应用到 ListBox 时,右下角有一个角,我无法找到任何方法来覆盖它。
不幸的是,在我获得更多积分之前,我无法发布图片。但是我指的角落是当垂直和水平滚动条都出现时,右下角有一个空间填充了我无法覆盖的灰白色
这是我使用 Blend 为 ScrollViewer 获得的模板代码的一部分。我在右下角添加了一个矩形并将填充设置为红色。您可以以相同的方式对其进行样式设置,也可以使用 Grid.RowSpan="2" 为 VerticalScrollBar(第一个)或 Grid.ColumnSpan="2" 为 HorizontalScrollBar(第二个)扩展其中一个 ScrollBar 以覆盖空间。
<Style TargetType="{x:Type ScrollViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollContentPresenter Grid.Column="0"/>
<ScrollBar Name="PART_VerticalScrollBar" Grid.Row="0" Grid.Column="1" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
<ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Value="{TemplateBinding HorizontalOffset}" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
<Rectangle Grid.Row="1" Grid.Column="1" Fill="Red"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
另外两个解决方案有效。
1) 矩形颜色是动态的,键为“SystemColors.ControlBrushKey”。您可以在自定义样式或 ScrollViewer 控件(或其祖先之一)的资源中覆盖此键。资料来源:MSDN 上的这个问题。
例子 :
<!-- In a style -->
<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
<!-- Or in the control -->
<ScrollViewer>
<ScrollViewer.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</ScrollViewer.Resources>
</ScrollViewer>
2)设置(与上面相同的位置)Rectangle
具有“隐藏”可见性的样式。警告:如果矩形包含在控件的后代中,这将产生副作用。
<Style x:Key="MyCustomScrollViewer" TargetType="{x:Type ScrollViewer}">
<Style.Resources>
<!-- 'BasedOn' can be omitted -->
<Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</Style.Resources>
</Style>
有两件事可能会有所帮助:
1) 使用Snoop探索应用程序的元素树,这可能有助于发现问题。
2) 根据您启动控件的方式,您可能会考虑从标准 ListBox 的副本开始。当我从空模板或部分模板开始设置样式时,我发现某些控件存在问题。
希望有帮助