【发布时间】:2017-12-16 17:00:07
【问题描述】:
对于 WPF 文本框控件,我在 app.xaml 中使用 XAML 样式设置 FontSize,如下所示:
<System:Double x:Key="FontSizeVal">12</System:Double>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="{DynamicResource FontSizeVal}"/>
</Style>
我想从 Code Behind 更改 FontSizeVal。我尝试使用下面的代码,但它不起作用(System.InvalidCastException: 'Specified cast is not valid.'):
App.Current.Resources["FontSizeVal"] = 10;
如何在代码中而不是在 XAML 中设置 FontSizeVal?
更新:
我的问题解决了,我改变了:
10
到
10.0
tnx 到 @ash
【问题讨论】:
-
“不工作”没有解释程序中的发生了什么。它会抛出运行时异常吗?或者 UI 中有一个 TextBlock 并且它仍然具有相同的 12 字体大小?还是别的什么?请澄清
-
System.InvalidCastException: '指定的演员表无效。'
-
10文字在这里被解释为int。试试10.0 -
tnx 这对我有用。
标签: c# wpf resourcedictionary wpf-style