我正在创建一个WPF应用程序,我想知道最好的方式,能够改变每个元素的ui字体大小。是否创建资源字典并设置Styles来设置我使用的所有控件的字体大小?
最佳实践是什么?
发布于 2009-05-21 17:57:48
我会这样做:
<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,也可以扩展基本样式)
发布于 2010-08-13 17:02:40
FontSizeProperty是从父控件继承的。所以你只需要改变你的主窗口的FontSize。
如果你不需要动态行为,这应该是可行的:
将窗口样式添加到ResourceDictionary
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="15" />
</Style>
将样式应用于主窗体(将不会隐式应用,因为它是派生类型)
Style = (Style)FindResource(typeof (Window));
发布于 2014-06-06 19:44:52
<Window>
有一个属性FontSize。
因此,如果您想要更改该窗口中所有元素的字体大小,则可以在元素中设置所需的字体大小。
<Window FontSize="12">
</Window>
https://stackoverflow.com/questions/893428
复制相似问题