【发布时间】:2019-05-24 04:28:17
【问题描述】:
我正在尝试覆盖 WPF 应用程序的垂直滚动条的宽度。将以下代码添加到 App.xaml 引用的资源字典中很有效:
<sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">50</sys:Double>
但是,我需要从应用程序的其他地方的代码隐藏中访问该值,因此我想将其设置为代码隐藏常量。
public static class MyConstants
{
public static double ScrollBarWidth = 50;
但是如何在 xaml 中将此值设置为双精度值? 我已经尝试了所有这些都没有成功:
<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}" />
【问题讨论】:
-
绑定只能在属性上完成。
ScrollBarWidth {get; set;} = 50;但我不确定静态值。也许你应该尝试 -
@NawedNabiZada 我实际上无法将绑定设置为资源句号:
The property 'Binding' was not found in type 'Double'。也许我只需要找到魔术关键字来分配它?