我正在创建一个WPF应用程序,想知道如何以最佳方式更改UI中每个元素的字体大小。 我需要创建资源字典并设置样式来设置所有使用的控件的字体大小吗?什么是最佳实践?
我会这样做:
<Window.Resources>
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontSize" Value="100" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
<!-- ComboBox, RadioButton, CheckBox, etc... -->
</Window.Resources>
这样,如果我想更改所有控件,我只需要更改“baseStyle”样式,其余控件将从它继承。(这就是BasedOn属性所做的事情,如果在继承样式中创建其他Setter,则还可以扩展基本样式)
如果您不需要动态行为,则应该可行:
向ResourceDictionary添加一个Window样式。
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="15" />
</Style>
将样式应用于您的主表单(不会隐式应用,因为它是派生类型)。 Style = (Style)FindResource(typeof (Window));
<Window FontSize="15">
呢? - mins<Style x:Key="defaultStyles">...
and <Window Style="{StaticResource defaultStyles}"></Window>
just to keep it in the xaml
files. - Get Off My Lawn<Window>
有一个属性FontSize。因此,如果您想在窗口内所有元素中更改字体大小,可以在< code > Window 元素中设置所需的字体大小。
<Window FontSize="12">
</Window>
另一种选择是将FontFamily和FontSize定义为资源。
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>
这样你就可以在setter方法中使用它们。
Application.Current.MainWindow.FontSize = _appBodyFontSize;
通过这种方式,您可以在运行时更改字体大小。
TextElement.FontSize
是一个继承属性,这意味着您可以在根元素上简单地设置字体大小,所有子元素将使用该大小(只要您不手动更改它们)。
对于WPF中的任何样式,您都应该有一个单独的资源字典,其中包含您应用程序的样式。
如果您想要在整个应用程序中重复使用单个字体大小,则只需为该字体大小创建样式。您可以为其指定唯一的名称/键来显式使用,也可以设置一个目标类型,该类型将贯穿整个应用程序。
显式键:
<Style
x:Key="MyFontSize"
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
</Style>
<Control
Style="{StaticResource MyFontSize}" />
*请注意,此样式可用于具有内容呈现器的控件
对于应用程序中的所有文本块:
<Style
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
</Style>
<TextBlock
Text="This text will be size 10" />
FontSizeProperty
的目的相矛盾。根据原则,依赖属性可以继承于逻辑树,因此,除非有特定的需要(例如仅在中间节点上具有不同的字体大小),否则在根元素上设置字体大小就足够了。更新该值将触发所有子级的更新。 - minsTextElement.FontSizeProperty.OverrideMetadata(
typeof(TextElement),
new FrameworkPropertyMetadata(16.0));
TextBlock.FontSizeProperty.OverrideMetadata(
typeof(TextBlock),
new FrameworkPropertyMetadata(16.0));
TextBlock
元素上使用<Style>
要好得多,因为否则你将不得不在每个覆盖它的地方使用BasedOn
! - bytecode77使用Ctrl + 鼠标滚轮全局动态更改字体大小:
XAML:
<Window Name="MainWindow" ... PreviewMouseWheel="MainWindow_PreviewMouseWheel">
代码后台:
private void MainWindow_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
{
if (e.Delta > 0)
++mainCtrl.FontSize;
if (e.Delta < 0 && mainCtrl.FontSize > 1)
--mainCtrl.FontSize;
}
}
<Application.Resources>
<Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
<Setter Property="FontSize" Value="28"/>
</Style>
</Application.Resources>
为了在窗口级别上实现无障碍性,您可以在 XAML 文件中为 Window 定义资源。
<Window.Resources>
<Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
<Setter Property="FontSize" Value="28"/>
</Style>
</Window.Resources>
<DockPanel.Resources>
<Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
<Setter Property="FontSize" Value="28"/>
</Style>
</DockPanel.Resources>