3

I have an application, which uses WPF on windows as toolkit framework. Now I would like to set the width of scrollbars for my ScrollViewers programmatically. I found many examples to set the width via XAML. But how can I define the width of scrollbars programmatically?

Unfortunately I could not find any property or method on ScrollViewer to set the width of scrollbars.

var viewer = new ScrollViewer();
viewer.MagicProperty = 42; // Does not exist

Also all properties on SystemParameters are unfortunately read-only.

VerticalScrollBarWidth.VerticalScrollBarWidth = 42; // Read-only

Edit: WPF is only one of multiple toolkit frameworks in my application. I use a custom GUI abstraction layer for supporting Windows (WPF), Linux (GTK#) and MacOS X (in future). My user interface is encapsulated in an OS independent way. Therefore it makes no sense to use XAML.

Martin
  • 598
  • 1
  • 4
  • 27

1 Answers1

1

Easiest way is to set x:Name property, then you can access ScrollViewer in your code.

Or use Binding: http://www.tutorialspoint.com/wpf/wpf_data_binding.htm

Binding will be useful if you want to manipulate with multile ScrollViewers and set same values.

EDIT: You can create ScrollViewer in your code and then set its parameters. But you need a way to insert it into VisualTree among other controls. So you need to get instance of some container and then use its Children.Add() method

However I'd really recommend to use as much XAML as you can and leave your code for application logic, not the UI building.

EDIT 2: Can you try:

            Style myStyle = new Style(typeof(ScrollBar));
            myStyle.Setters.Add(new Setter(WidthProperty, 40));

            scrollViewer.Style = myStyle;

EDIT 3: I found a solution. You can add ResourceDictionary.xaml and add this style to it:

 <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <Setter Property="MinWidth" Value="35" />
        <Setter Property="Width" Value="35" />
    </Style>

Then load it at runtime like so:

Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri(@"Dictionary.xaml", UriKind.Relative)) as
                ResourceDictionary);
Filip
  • 1,824
  • 4
  • 18
  • 37
  • Thanks for your answer. Unfortunately I need a way without any XAML. – Martin Feb 08 '16 at 15:57
  • Can you please clarify what you mean by "without any XAML"? My approach is programmatic, you just need a way to tell which scrollViewer to change. – Filip Feb 08 '16 at 15:58
  • The linked website shows a data binding tutorial, based on XAML examples. I need pure C# or VB.NET code, like _new ScrollViewer().ScrollBarWith = 42_ or _SystemParameters.ScrollBarWith = 42_. Both examples don't work of course. But I hope that they clarifies for what I'm looking. – Martin Feb 08 '16 at 16:06
  • That is certainly possible, just unnecessary complicated. If you don't want can't set x:Name then you must find your ScrolViewer using VisualTree - http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type – Filip Feb 08 '16 at 16:11
  • I have all ScrollViewer instances. This is not the problem. But I cannot find any property or method to set the scroll bar width. In SystemParameters all properties are unfortunately read-only. I have nothing against x:Name if I would know a way to use it without having any XAML files. – Martin Feb 08 '16 at 16:18
  • I have extended my question. I hope that you can understand my problem better now. – Martin Feb 08 '16 at 17:00
  • Throws an InvalidOperationException: "TargetType 'ScrollBar'" does not match the element type "ScrollViewer" (original text translated into English) – Martin Feb 08 '16 at 18:35
  • Thank you very much! Until now, the API of WPF was very logical and self-describing. Therefore I haven't expected that changing the scroll bar width could require such a workaround. – Martin Feb 10 '16 at 12:56