[英]Change Font Size of all UI Elements in using Static Resource from App.xaml in WPF
我需要更改应用程序中所有文本的字体大小。
我尝试过如下操作,但这不起作用:-
<Style x:Key="fontsize" TargetType="{x:Type FrameworkElement}">
<Setter Property="Control.FontSize" Value="20"/>
</Style>
<Style TargetType="{x:Type FrameworkElement}" BasedOn="{StaticResource fontsize}"/>
当我尝试如下设置时,效果很好,但不会应用于所有元素,需要将其应用于所有不同类型的元素。
<Style TargetType="TextBlock" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DataGridCell" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="MenuItem" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DatePicker" BasedOn="{StaticResource fontsize}"/>
另外我想问一下,有没有一种方法可以覆盖特定元素的全局样式,例如用户控件上的标题文本应该具有不同的大小?
在 App.xaml
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
在 App.xaml 中为 window 创建全局样式。
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</Application.Resources>
并为所需的 windows 设置该样式。
<Window x:Class="YourNamespace.MainWindow" Style="{StaticResource WindowStyle}".....>
用于覆盖用户控件的样式
<local:UserControl1>
<local:UserControl1.Style>
<Style TargetType="UserControl">
<Setter Property="FontSize" Value="10"/>
</Style>
</local:UserControl1.Style>
</local:UserControl1>
这涉及到两个控制。 您可能在想“嘿,这个单元格或那个日历怎么样”。 他们的模板在文本块中显示文本。 当您在菜单项或 label 上的内容上设置 Header 时,您会生成一个文本块。
因此,您“只”需要在文本块和文本框上设置样式:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
话说回来。
正如克莱门斯指出的那样。
字体大小和样式依赖属性被标记为继承,所以如果你只有主窗口,那么你可以设置它。
但是,当您设置内容时,label 最终会在其中包含一个文本块,这不仅仅是“显而易见的”。 同样,一个菜单项和 header。 因此,我认为值得发布这个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.