我正在创建一个WPF应用程序,我想知道能够更改ui中每个元素的字体大小的最佳方法。我是否创建资源字典并设置样式以设置我使用的所有控件的字体大小?
最佳做法是什么?
答案 0 :(得分:83)
我这样做:
<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”样式,其余部分将继承它。 (那就是那些基于AnyOn属性的东西,如果你在继承的样式中创建其他setter,你也可以扩展基本样式)
答案 1 :(得分:37)
FontSizeProperty继承自Parent Control。所以你只需要更改主窗口的FontSize。
如果您不需要动态行为,这应该有效:
将Window样式添加到ResourceDictionary
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="15" />
</Style>
将样式应用于主窗体(不会隐式应用,因为它是派生类型)
Style = (Style)FindResource(typeof (Window));
答案 2 :(得分:25)
另一种选择是将FontFamily和FontSize定义为资源。
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>
这样你就可以在你的制定者中使用它们。
答案 3 :(得分:20)
<Window>
有一个属性FontSize。
因此,如果要更改该窗口中所有元素的fontsize,可以在元素中设置所需的fontsize。
<Window FontSize="12">
</Window>
答案 4 :(得分:8)
Application.Current.MainWindow.FontSize = _appBodyFontSize;
这样您也可以在运行时更改字体大小。
答案 5 :(得分:4)
TextElement.FontSize
是一个继承属性,这意味着您只需在根元素处设置字体大小,并且所有子元素都将使用该大小(只要您不手动更改它们)
答案 6 :(得分:3)
对于WPF中的任何样式,您应该有一个单独的资源字典,其中包含应用程序的样式。
如果您希望在整个应用程序中重复使用单个字体大小,则只需为该字体大小创建样式。您可以为其指定一个唯一的名称/密钥,也可以设置一个将在整个应用程序中超越的targetType。
明确密钥:
<Style
x:Key="MyFontSize"
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
</Style>
<Control
Style="{StaticResource MyFontSize}" />
*请注意,此样式可以与包含contentPresenters的控件一起使用
对于应用中的所有文本块:
<Style
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
</Style>
<TextBlock
Text="This text will be size 10" />
答案 7 :(得分:1)
如果您需要以编程方式更改全局FontSize,而不是静态(XAML),要对所有窗口应用一次,您可以执行以下操作:
TextElement.FontSizeProperty.OverrideMetadata(
typeof(TextElement),
new FrameworkPropertyMetadata(16.0));
TextBlock.FontSizeProperty.OverrideMetadata(
typeof(TextBlock),
new FrameworkPropertyMetadata(16.0));
此值适用于任何TextBlock,Labels和几乎任何窗口中的任何文本,而它没有定义明确的FontSize。但是这对TextBox没有影响,你必须为它或任何其他特殊控件编写类似的代码。
答案 8 :(得分:1)
要使用ctrl-mousewheel动态地全局更改字体大小,请执行以下操作:
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;
}
}
答案 9 :(得分:0)
在XAML中使用资源是必经之路。尽管对于这个问题有很多很好的答案,但我想在资源范围内加上两美分。
对于项目的所有Windows和用户控件中的全局可访问性,您可以将资源放在 App.xaml 文件中
<Application.Resources>
<Style TargetType="{x:Type Control}" x:Key="GlobalFontSize">
<Setter Property="FontSize" Value="28"/>
</Style>
</Application.Resources>
要获得Window级别的可访问性,您可以将资源保存在 Window
的xaml文件中<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>
答案 10 :(得分:0)
让我们来点 BLACK MAGIC 的东西:
<Application.Resources>
<sys:Double xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Key="GlobalFontSize">12</sys:Double>
</Application.Resources>
public static double GlobalFontSize
{
get => (double)Current.Resources["GlobalFontSize"];
set => Current.Resources["GlobalFontSize"] = value;
}
DynamicResource
FontSize="{DynamicResource GlobalFontSize}"
App.GlobalFontSize
以更改值,binding
没问题!App.GlobalFontSize = 20;
//Or
{Binding Path=(local:App.GlobalFontSize)}