我有一个 ScrollViewer 的样式,我希望即使没有可滚动的内容,垂直滚动条也可见,但我似乎无法让它工作。从我在前面的问题中发现的是,您所需要做的就是将 VerticalScrollBarVisibility 设置为 Visible,但它仍然以这种方式隐藏。我在这里缺少什么?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="BorderColor">#a9aba9</Color>
<Color x:Key="ThumbColor">#868786</Color>
<Style x:Key="PropertiesUCScrollViewer"
TargetType="{x:Type ScrollViewer}">
<Setter Property="VerticalScrollBarVisibility"
Value="Visible" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"
MaxWidth="15" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Column="0"
BorderThickness="0.25"
Margin="15 0 0 0">
<ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" />
</Border>
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Column="1"
Style="{DynamicResource PropertiesUCScrollBar}"
Value="{TemplateBinding VerticalOffset}"
Maximum="{TemplateBinding ScrollableHeight}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="Visible"
Width="1" />
<ScrollBar x:Name="PART_HorizontalScrollBar"
Orientation="Horizontal"
Grid.Row="1"
Grid.Column="1"
Style="{DynamicResource PropertiesUCScrollBar}"
Value="{TemplateBinding HorizontalOffset}"
Maximum="{TemplateBinding ScrollableWidth}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="Collapsed" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="PropertiesUCScrollBarThumb"
TargetType="{x:Type Thumb}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="IsTabStop"
Value="false" />
<Setter Property="Focusable"
Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border CornerRadius="3"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1"
Width="8" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="PropertiesUCVerticalScrollBar"
TargetType="{x:Type ScrollBar}">
<Grid>
<Border CornerRadius="2"
Background="Transparent" />
<Track x:Name="PART_Track"
Grid.Row="1"
IsDirectionReversed="true">
<Track.Thumb>
<Thumb Style="{StaticResource PropertiesUCScrollBarThumb}">
<Thumb.BorderBrush>
<SolidColorBrush Color="{DynamicResource ThumbColor}" />
</Thumb.BorderBrush>
<Thumb.Background>
<SolidColorBrush Color="{DynamicResource ThumbColor}" />
</Thumb.Background>
</Thumb>
</Track.Thumb>
</Track>
</Grid>
</ControlTemplate>
<Style x:Key="PropertiesUCScrollBar"
TargetType="{x:Type ScrollBar}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Style.Triggers>
<Trigger Property="Orientation"
Value="Vertical">
<Setter Property="Height"
Value="Auto" />
<Setter Property="Template"
Value="{StaticResource PropertiesUCVerticalScrollBar}" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
当内容适合容器时,理论上滚动滑块的高度为零。所以用户看到的是滚动条Track。
因此,除了将 Scrollbar 可见性设置为“visible”之外,您应该做的是设置 Track 的 background color。
这是一个模型和链接到滚动条样式,您可以自己检查:
<Style TargetType="ScrollBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollBar">
<!-- You Can set the Track Background Color -->
<Track Name="PART_Track" Background="Red">
<!-- ... -->
<Track.Thumb>
<!--You Can set the height of the Thumb.-->
<Thumb Height="30"/>
</Track.Thumb>
</Track>
<!-- ... -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>