0

How can I set the scrollbar width application wide from code behind? I found some Examples in this post (How to increase scrollbar width in WPF ScrollViewer?), but only in XAML and not dynamically.

The important thing for me is that I am able to change the scrollbar width when the programm is running. So whatever I do, it must be possible to update the value over and over again.

<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled" Value="True" />
<Style.Triggers>
    <Trigger Property="Orientation" Value="Horizontal">
        <Setter Property="Height" Value="40" />
        <Setter Property="MinHeight" Value="40" />
    </Trigger>
    <Trigger Property="Orientation" Value="Vertical">
        <Setter Property="Width" Value="40" />
        <Setter Property="MinWidth" Value="40" />
    </Trigger>
</Style.Triggers>

or

<Application
xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
>
<Application.Resources>
    <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">50</sys:Double>
    <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">50</sys:Double>
</Application.Resources>

be_mi
  • 529
  • 6
  • 21

1 Answers1

0

Maybe this approach will help you: It uses DataBinding (which should the way to go in WPF) and gives you the oportunity to change the width in codebehind.

XAML

<ScrollViewer>
        <ScrollViewer.Resources>
            <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
                <Setter Property="Width" Value="{Binding MyWidth,Mode=OneWay}" />
            </Style>
        </ScrollViewer.Resources>
</ScrollViewer>

CodeBehind

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private double myWidth;
    public double MyWidth
    {
        get { return myWidth; }
        set 
        {
            if (value != this.myWidth)
            {
                myWidth = value; 
                NotifyPropertyChanged("MyWidth");
            }
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        //set the width here in code behind
        this.MyWidth = 200;
    }

    protected virtual void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Don't forget to Implement the INotifyPropertyChanged - Interface

wulf11
  • 79
  • 8
  • I tried it, but I have placed it into to have a available everywhere in my application. Tried to access it via , but it's not working. Isn't it possible to bind to the App object? – be_mi Mar 31 '19 at 09:49
  • maybe this link will help you: https://stackoverflow.com/questions/1389503/bind-to-property-in-app-xaml-cs – wulf11 Mar 31 '19 at 10:20