Reputation: 5665
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}" />
Upvotes: 3
Views: 2777
Reputation: 2994
Using the x:Static
markup extension without MemberType
works for me:
<x:Static x:Key="myKey" Member="local:MyConstants.ScrollBarWidth"></x:Static>
Upvotes: 0
Reputation: 12276
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.
Upvotes: 1
Reputation: 134
This seems to work for me
<x:Static x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}" Member="local:Constants.scrollBarWidth" MemberType="{x:Type sys:Double}"></x:Static>
Upvotes: 2