WPF使用样式设置滚动条缩略图大小会从控件中删除所有样式

时间:2017-02-04 22:59:38

标签: c# wpf xaml styles

我正在尝试使用以下代码建议将滚动条的最小拇指大小更改为不合理的小。所有使用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>

1 个答案:

答案 0 :(得分:0)

有两种方式:硬方式和简单方式。

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

  1. 在“XAML设计”窗口中,选择要调整的控件。在您的情况下,您应该创建一个<ScrollBar/>并在设计器中选择它。
  2. 在主菜单中,单击格式 - 编辑样式 - 编辑副本... - 全部应用 - 确定。
  3. 根据需要修改样式。
  4. 现在很简单:

    1. 创建BasedOn默认样式的新样式。
    2. 通过将默认样式放在<Style.Resources>
    3. 中来覆盖默认样式使用的资源

      以下是示例:

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

      这是你得到的截图:

      enter image description here

      希望这有帮助!