WPF 设置带有样式的滚动条拇指大小会从控件中删除所有样式
WPF Setting scrollbar thumb size with style removes all style from control
我正在尝试使用建议的以下代码将滚动条的最小拇指大小更改为不至于过小。所有使用 StaticResource 的实例都说 "The resource "x" 无法解析。"我试图将其更改为 DynamicResource 以消除错误并正常运行,但我得到的东西看起来几乎不像滚动条。我已经尝试了另一个建议 here,我得到了类似的结果,但看起来几乎不像滚动条。我还应该注意,我依赖于文本框 class 中实现的滚动条,所以我没有像其他人建议的那样扩展滚动条的奢侈。
如何在不完全破坏控件样式的情况下使用样式来修复文本框滚动条的最小缩略图大小?
以下代码来自 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的基本例子。
<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>
有两种方式,一种是困难的,一种是简单的。
让我们从困难的开始,这反过来更灵活。为了调整控件的默认样式,您应该复制整个样式,包括它引用的所有资源。您无需在 MSDN 中挖掘样式,只需使用 Visual Studio:
- 在 XAML 中设计 window,select 您想要调整的控件。在您的情况下,您应该在设计器中创建
<ScrollBar/>
和 select。
- 在主菜单中,点击格式-编辑样式-编辑副本...-全部应用-确定。
- 根据需要修改样式。
现在是简单的:
- 创建一个新样式
BasedOn
默认样式。
- 通过将默认样式使用的资源放在
<Style.Resources>
中来覆盖它们。
示例如下:
<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>
这是您得到的屏幕截图:
希望对您有所帮助!
我正在尝试使用建议的以下代码将滚动条的最小拇指大小更改为不至于过小。所有使用 StaticResource 的实例都说 "The resource "x" 无法解析。"我试图将其更改为 DynamicResource 以消除错误并正常运行,但我得到的东西看起来几乎不像滚动条。我已经尝试了另一个建议 here,我得到了类似的结果,但看起来几乎不像滚动条。我还应该注意,我依赖于文本框 class 中实现的滚动条,所以我没有像其他人建议的那样扩展滚动条的奢侈。
如何在不完全破坏控件样式的情况下使用样式来修复文本框滚动条的最小缩略图大小?
以下代码来自 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的基本例子。
<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>
有两种方式,一种是困难的,一种是简单的。
让我们从困难的开始,这反过来更灵活。为了调整控件的默认样式,您应该复制整个样式,包括它引用的所有资源。您无需在 MSDN 中挖掘样式,只需使用 Visual Studio:
- 在 XAML 中设计 window,select 您想要调整的控件。在您的情况下,您应该在设计器中创建
<ScrollBar/>
和 select。 - 在主菜单中,点击格式-编辑样式-编辑副本...-全部应用-确定。
- 根据需要修改样式。
现在是简单的:
- 创建一个新样式
BasedOn
默认样式。 - 通过将默认样式使用的资源放在
<Style.Resources>
中来覆盖它们。
示例如下:
<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>
这是您得到的屏幕截图:
希望对您有所帮助!