XAML 如何使我的ScrollViewer垂直滚动条始终可见?

cdmah0mi  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(100)

我有一个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>

字符串

pdkcd3nj

pdkcd3nj1#

当内容适合容器时,滚动块的高度理论上为零。因此用户可以看到的是滚动条Track
因此,除了将 Scrollbar 可见性设置为“visible"之外,您应该做的是设置轨道的背景颜色
这里有一个模型和link to a scrollbar style,你可以自己检查:

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

字符串

相关问题