WPF - 使用样式更改滚动条的文本框边距

WPF - Change Scrollbar's margin of a TextBox using Styles

我为 Scrollbar 定义了一个隐式样式并设置了一些属性,我将它用于大多数 ScrollView。部分样式为:

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <Setter Property="Background" Value="#FF283542" />
        <Setter Property="Margin" Value="0,-32,0,0" />
        <Setter Property="Width" Value="5" />
</Style>

但我有一些文本框,我希望它们的滚动条具有相同的隐式滚动条样式,但边距不同。

我可以通过向每个 TextBox 添加 Resources 并覆盖隐式 ScrollBar 样式来实现,例如:

    <TextBox Style="{StaticResource big-text-style}">
                        <TextBox.Resources>
                            <Style TargetType="{x:Type ScrollBar}" 
                                   BasedOn="{StaticResource {x:Type ScrollBar}}">
                                <Setter Property="Margin" Value="0"/>
                            </Style>
                        </TextBox.Resources>
    </TextBox>

这段代码提供了我想要的功能。但是这种方法的问题是我必须为每个 TextBox 编写这些代码行!如果我能把它作为 TextBox 样式本身的一部分,那就更好了。

我想知道有没有办法将它放在 TextBox 的样式中,以便每个具有大文本样式(例如)的 TextBox 都具有覆盖的 ScrollBar?

或者有没有更好的方法来实现这种事情?

感谢您的帮助!

您可以将覆盖 ScrollBar 样式添加到 TextBox 样式本身的 Resources

<Style x:Key="big-text-style" TargetType="TextBox">
    <Style.Resources>
        <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
            <Setter Property="Margin" Value="0"/>
        </Style>
    </Style.Resources>
</Style>