wpf 在app.xaml 中设定了全局的textblock fontsize,大部分控件的字体都受影响了

<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontSize" Value="15"/>
</Style>

项目中的DatePicker控件字体也受影响了,为DatePicker控件指定style ,都没用,求解!

第1个回答  2014-06-19
添加一个key就行了:
<Style x:Key="CustomTextBlock" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontSize" Value="15"/>
</Style>
使用时指定Key,未指定的就不会有影响了:
<TextBlock Style="{StaticResource CustomTextBlock}"/>
希望对你有帮助,还有疑问请追问或是Hi追问

这样是没问题,但是项目中太多TextBlock , 每个都加上Style, 太麻烦了,有没有办法让DatePicker 在有Style的情况下也能改变字体

追答

给DatePicker写个Style不行吗?

追问

试了, 不行呢。

追答

试了一下,重写DatePicker应该更麻烦一些,不仅如此,还得重写Calendar,还不如就对TextBlock指定Key了,总之我觉得你使用全局的TextBlock这个用法有问题,因为会影响其它的很多控件

第2个回答  2022-07-29
在每个窗体或者page的xaml文件中添加:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</Window.Resources>
不知道是什么原因,单独再每个xaml的window.resource中设置样式,其他控件不受影响。
但在app.xmal中设置textblock样式,所有其他控件都改变了。

有知道原理的告知下。
相似回答