Case
I've defined a series of GridLenghts as a resource in my WPF Window:
<w:GridLength x:Key="ScrollBarRowHeight">17</w:GridLength>
Since this scrollbar height is variable dependent on the operating system used, I'd like to refactor this line of code to use the static parameter value SystemParameters.HorizontalScrollBarHeight.
Problem
I've tried both these lines:
<w:GridLength x:Key="ScrollBarRowHeight"><DynamicResource Key="{x:Static System.Windows.SystemParameters.CaptionHeightKey}" /></w:GridLength>
<w:GridLength x:Key="ScrollBarRowHeight"><x:Static x:Key="System.Windows.SystemParameters.HorizontalScrollBarHeight" /></w:GridLength>
Resulting both in the same compile-time error:
Cannot add content to object of type 'System.Windows.GridLength'.
Questions
- Is it possible to do this declaratively in XAML?
- If yes, how?
- If no, is there a neat solution which does not include code-behind?
Thanks in advance!
I'm wondering why don't you just use the
SystemParameters.HorizontalScrollBarHeight
value directly in your XAML, instead of trying to duplicate its value? (Added from comment)On the
SystemParameters.HorizontalScrollBarHeight
page that you provided a link to, there is a code example which shows you exactly how to use the variousSystemParameters
properties in both XAML and code:From the linked page:
So if you want the values to update while the application is running, then you should be able to use these properties in a
Binding
like this:You should also be able to use it as a
DynamicResource
in this way: