繁体   English   中英

为GridLength资源分配静态值

[英]Assign static value to GridLength resource

案件

我已经在WPF窗口中定义了一系列GridLenghts作为资源:

<w:GridLength x:Key="ScrollBarRowHeight">17</w:GridLength>

由于此滚动条的高度是可变的,取决于所使用的操作系统,因此我想重构此代码行以使用静态参数值SystemParameters.Horizo​​ntalScrollBarHeight

问题

我已经尝试了以下两行:

<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>

两者都导致相同的编译时错误:

Cannot add content to object of type 'System.Windows.GridLength'.

问题

  • 是否可以在XAML中以声明方式执行此操作?
  • 如果是,怎么办?
  • 如果否,是否有一个不包含后台代码的简洁解决方案?

提前致谢!

我想知道为什么您不直接在XAML中直接使用SystemParameters.HorizontalScrollBarHeight值,而不是尝试复制其值? (从评论中添加)

SystemParameters.HorizontalScrollBarHeight 提供一个链接页面,有这表明你到底如何使用不同的代码示例SystemParameters两个XAML 代码属性:

<Button FontSize="8" Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="5"      
     HorizontalAlignment="Left" 
     Height="{x:Static SystemParameters.CaptionHeight}"
     Width="{x:Static SystemParameters.IconGridWidth}">
     SystemParameters
</Button>

...

Button btncsharp = new Button();
btncsharp.Content = "SystemParameters";
btncsharp.FontSize = 8;
btncsharp.Background = SystemColors.ControlDarkDarkBrush;
btncsharp.Height = SystemParameters.CaptionHeight;
btncsharp.Width = SystemParameters.IconGridWidth;
cv2.Children.Add(btncsharp);

从链接页面:

在XAML中,可以将SystemParameters的成员用作静态属性用法或动态资源引用(以静态属性值作为键)。 如果要在应用程序运行时自动更新基于系统的值,请使用动态资源引用。 否则,请使用静态引用。 资源密钥在属性名称后附加后缀密钥。

因此,如果您希望在应用程序运行时更新值,那么您应该能够在Binding使用这些属性,如下所示:

<Button FontSize="8" Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="5"      
     HorizontalAlignment="Left" 
     Height="{Binding Source={x:Static SystemParameters.CaptionHeight}}"
     Width="{Binding Source={x:Static SystemParameters.IconGridWidth}}">
     SystemParameters
</Button>

你也应该能够使用它作为一个DynamicResource这样:

Property="{DynamicResource {x:Static SystemParameters.CaptionHeight}}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM