Jaguel
Jaguel

Reputation: 23

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

I have a defined an implicit style for Scrollbar and set some properties and I use it for most ScrollViewrs. Part of the Style is :

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

But I have some TextBoxes which I want them to have the same implicit Scrollbar style for their Scrollbars but with a diffrent margin.

I can do it by adding Resources to every TextBox and override the implicit ScrollBar style like :

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

This code gives me the functionality I want. But the problem with this approach is that I have to write these lines of code of every TextBox! It would be much better if I could put this as part of the TextBox style itself.

I wonder is there a way to put it in the TextBox's Style so that every TextBox which has big-text-style(for example), have the overriden ScrollBar ?

Or is there a better way to implement this kind of thing ?

Thank you for the help!

Upvotes: 2

Views: 727

Answers (1)

V.Leon
V.Leon

Reputation: 586

You can add the overriding ScrollBar style to the Resources of the TextBox style itself:

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

Upvotes: 2

Related Questions