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

时间:2021-09-05 14:14:26

I'm attempting to use the following code suggested to change the minimum thumb size for scrollbars to not be unreasonably small. All of the instances using StaticResource say "The resource "x" cannot be resolved." I've attempted to change it to DynamicResource which removes the errors and properly runs but instead I get something that barely looks like a scrollbar. I've tried another suggestion here and I got a similar result of something that barely looks like a scrollbar. I should also note that I'm relying on the scrollbar that comes implemented in the textbox class so I don't have the luxury of extending the scrollbar like others have suggested.

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

How would I go about using styles to fix the minimum thumb size of my textbox scrollbar without completely destroying the style of the control?

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

The following code was gotten from this Microsoft page.

以下代码来自此Microsoft页面。

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

The following is a basic example of the window.

以下是窗口的基本示例。

<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 个解决方案

#1


0  

There are two ways: the hard one, and the easy one.

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

Let's start with the hard one, which, in its turn, is more flexible. In order to tweak the default style of a control, you should copy the whole style, including all the resources it refers to. You don't need to dig in MSDN for a style, just use Visual Studio:

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

  1. In XAML Design window, select a control you want to tweak. In your case, you should create a <ScrollBar/> and select it in the designer.
  2. 在“XAML设计”窗口中,选择要调整的控件。在您的情况下,您应该创建一个 并在设计器中选择它。

  3. In the main menu, click Format – Edit Style – Edit a Copy... – Apply To All – OK.
  4. 在主菜单中,单击格式 - 编辑样式 - 编辑副本... - 全部应用 - 确定。

  5. Modify the style as needed.
  6. 根据需要修改样式。

And now the easy one:

而现在很简单:

  1. Create a new style that is BasedOn a default style.
  2. 创建一个基于默认样式的新样式。

  3. Override the resources that are used by the default style by putting them in the <Style.Resources>.
  4. 通过将它们放在 中来覆盖默认样式使用的资源。

Here is the example:

这是一个例子:

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

And here is a screenshot of what you get:

这是你得到的截图:

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

Hope this helps!

希望这可以帮助!

#1


0  

There are two ways: the hard one, and the easy one.

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

Let's start with the hard one, which, in its turn, is more flexible. In order to tweak the default style of a control, you should copy the whole style, including all the resources it refers to. You don't need to dig in MSDN for a style, just use Visual Studio:

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

  1. In XAML Design window, select a control you want to tweak. In your case, you should create a <ScrollBar/> and select it in the designer.
  2. 在“XAML设计”窗口中,选择要调整的控件。在您的情况下,您应该创建一个 并在设计器中选择它。

  3. In the main menu, click Format – Edit Style – Edit a Copy... – Apply To All – OK.
  4. 在主菜单中,单击格式 - 编辑样式 - 编辑副本... - 全部应用 - 确定。

  5. Modify the style as needed.
  6. 根据需要修改样式。

And now the easy one:

而现在很简单:

  1. Create a new style that is BasedOn a default style.
  2. 创建一个基于默认样式的新样式。

  3. Override the resources that are used by the default style by putting them in the <Style.Resources>.
  4. 通过将它们放在 中来覆盖默认样式使用的资源。

Here is the example:

这是一个例子:

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

And here is a screenshot of what you get:

这是你得到的截图:

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

Hope this helps!

希望这可以帮助!