如何在WPF ScrollViewer中增加滚动条宽度?

问题描述 投票:34回答:4

我正在小型设备上的触摸屏上工作,并且滚动条的自定义宽度不好,因为我的要求之一是一切都需要用手指手势才能完成。

如何设置WPF ScrollViewer滚动条的宽度?

请注意,我不想更改设备上所有滚动条的宽度(可通过Windows设置进行更改-仅更改应用程序中的滚动条。

wpf scrollbar width scrollviewer
4个回答
59
投票

ScrollBar模板可获取系统参数以确定其宽度/高度(取决于方向)。因此,您可以覆盖这些参数:

<ScrollViewer>
    <ScrollViewer.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">100</sys:Double>
    </ScrollViewer.Resources>
</ScrollViewer>

23
投票

肯特的答案还可以通过将其放置在App.xaml资源中,并通过指定水平高度键来轻松地应用于应用程序中的all滚动条。

<Application
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    ...
>
    <Application.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">50</sys:Double>
        <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">50</sys:Double>
    </Application.Resources>
</Application>

21
投票

这里是XAML解决方案:

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
    <Setter Property="Stylus.IsFlicksEnabled" Value="True" />
    <Style.Triggers>
        <Trigger Property="Orientation" Value="Horizontal">
            <Setter Property="Height" Value="40" />
            <Setter Property="MinHeight" Value="40" />
        </Trigger>
        <Trigger Property="Orientation" Value="Vertical">
            <Setter Property="Width" Value="40" />
            <Setter Property="MinWidth" Value="40" />
        </Trigger>
    </Style.Triggers>
</Style>

0
投票

并且,如果您不想使用XAML,则可以在Application的构造函数中进行操作,例如

using System.Windows;

public partial class App
{
    public App()
    {
        Resources.Add(SystemParameters.VerticalScrollBarWidthKey, 50d);
        Resources.Add(SystemParameters.HorizontalScrollBarHeightKey, 50d);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.