I am trying to override the width of the vertical scrollbar for a WPF application. Adding the following code to a resource dictionary referenced by the App.xaml works a treat:
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">50</sys:Double>
However, I need to access the value from code-behind elsewhere in the application, so I would like to set this to a code-behind constant.
public static class MyConstants
{
public static double ScrollBarWidth = 50;
But how do I set this value to the double in xaml? I've tried all of these without success:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:constants="clr-namespace:MyProject">
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}" Binding="{x:Static constants:MyConstants.ScrollBarWidth}" />
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}" Source="{x:Static constants:MyConstants.ScrollBarWidth}" />
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}" Value="{x:Static constants:MyConstants.ScrollBarWidth}" />
You could add the entry to your resources and get it back from there in code.
var key = SystemParameters.VerticalScrollBarWidthKey;
Application.Current.Resources[key] = 42d;
Double vsbWidth = (Double)Application.Current.Resources[key];
Console.WriteLine(vsbWidth.ToString());
If you wanted to override the value at (say) window scope you could do this.Resources rather than application.