【问题标题】:WPF Setting scrollbar thumb size with style removes all style from controlWPF 使用样式设置滚动条拇指大小会从控件中删除所有样式
【发布时间】:2017-02-04 22:59:37
【问题描述】:

我正在尝试使用建议的以下代码将滚动条的最小拇指大小更改为不会过小。所有使用 StaticResource 的实例都说“无法解析资源“x”。”我试图将其更改为 DynamicResource 以消除错误并正确运行,但我得到的东西几乎不像滚动条。我尝试了另一个建议here,我得到了类似的结果,看起来几乎不像滚动条。我还应该注意,我依赖于文本框类中实现的滚动条,所以我没有像其他人建议的那样扩展滚动条的奢侈。

如何在不完全破坏控件样式的情况下使用样式来修复文本框滚动条的最小拇指大小?

以下代码来自this Microsoft page

<Style TargetType="ScrollBar">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ScrollBar">
          <Grid Name="Bg"
                Background="{TemplateBinding Background}"
                SnapsToDevicePixels="true">
            <Grid.RowDefinitions>
              <RowDefinition MaxHeight="{DynamicResource 
              {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
              <RowDefinition Height="0.00001*"/>
              <RowDefinition MaxHeight="{DynamicResource 
              {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
            </Grid.RowDefinitions>
            <RepeatButton Style="{StaticResource ScrollBarButton}"
                          IsEnabled="{TemplateBinding IsMouseOver}"
                          Height="18"
                          Command="ScrollBar.LineUpCommand"
                          Content="M 0 4 L 8 4 L 4 0 Z" />
            <Track Name="PART_Track" 
                 IsDirectionReversed="true"
                 Grid.Row="1"
                 Grid.ZIndex="-1">
              <Track.Resources>
                <!-- Set the Thumb's minimum height to 50.
              The Thumb's minimum height is half the
              value of VerticalScrollBarButtonHeightKey. -->
                <sys:Double 
                  x:Key="{x:Static SystemParameters.VerticalScrollBarButtonHeightKey}">
                  100
                </sys:Double>
              </Track.Resources>
              <Track.DecreaseRepeatButton>
                <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}"
                              Command="ScrollBar.PageUpCommand"/>
              </Track.DecreaseRepeatButton>
              <Track.IncreaseRepeatButton>
                <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}"
                              Command="ScrollBar.PageDownCommand"/>
              </Track.IncreaseRepeatButton>
              <Track.Thumb>
                <Thumb/>
              </Track.Thumb>
            </Track>
            <RepeatButton 
              Grid.Row="2" 
              Style="{StaticResource ScrollBarButton}"
              Height="18"
              Command="ScrollBar.LineDownCommand"
              Content="M 0 0 L 4 4 L 8 0 Z"/>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger SourceName="PART_Track" 
                     Property="IsEnabled" Value="false">
              <Setter TargetName="PART_Track" 
                      Property="Visibility" Value="Hidden"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

以下是窗口的基本示例。

<Window x:Class="TriggersNotepad.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TriggersNotepad"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="Trigger's Notepad" Height="350" Width="525" ResizeMode="CanResizeWithGrip">
    <Window.Resources>
        Code above...
    </Window.Resources>
    <TextBox AcceptsReturn="True" UseLayoutRounding="False" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" BorderThickness="0"/>
</Window>

【问题讨论】:

    标签: c# wpf xaml styles


    【解决方案1】:

    有两种方法:一种是困难的,一种是容易的。

    让我们从硬的开始,它又更灵活。为了调整控件的默认样式,您应该复制整个样式,包括它引用的所有资源。您无需在 MSDN 中挖掘样式,只需使用 Visual Studio:

    1. 在 XAML 设计窗口中,选择要调整的控件。在您的情况下,您应该创建一个 &lt;ScrollBar/&gt; 并在设计器中选择它。
    2. 在主菜单中,点击格式——编辑样式——编辑副本...——全部应用——确定。
    3. 根据需要修改样式。

    现在是最简单的:

    1. 创建一个新样式,它是BasedOn 默认样式。
    2. 通过将默认样式使用的资源放在&lt;Style.Resources&gt; 中来覆盖它们。

    示例如下:

    <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
        <Style.Resources>
            <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarButtonHeightKey}">42</sys:Double>
            <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">42</sys:Double>
            <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">42</sys:Double>
            <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}">42</sys:Double>
        </Style.Resources>
    </Style>
    

    这是你得到的截图:

    希望这会有所帮助!

    【讨论】:

    • 这有一个问题。至少在 Windows 7 上,当您更改拇指大小时,上下/左右滚动按钮的大小也会增加。拇指现在是一个合理的尺寸,但其他按钮只是因为被拉伸而让它看起来更糟。 (注意我从 Vertical 中删除了 Width 属性,从 Horizo​​ntal 中删除了 Height 属性)
    • 不幸的是,Windows 7 主题 (Aero.NormalColor.xaml) 使用 Microsoft.Windows.Themes.ScrollChrome 来呈现拇指箭头,这不符合自定义拇指大小。您可以复制该样式,并将ScaleTransform 应用于RepeatButton 内的ScrollChrome
    • 谢谢。 :) 最后我确实选择了原始答案。我将它拉伸到足以获得正常的最小拇指大小。使用拉伸的箭头按钮并不太碍眼。
    猜你喜欢
    • 2013-08-24
    • 2018-06-03
    • 1970-01-01
    • 2011-02-13
    • 2012-04-16
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多