文本框设置默认字体
TextBox set default font
如何设置 TextBox
的默认字体?
对于 TextBlock
它是(取自 here):
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
尝试对TextBox
做同样的事情:
TextBox.FontFamilyProperty.OverrideMetadata(typeof(TextBox),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
将抛出:
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: The type initializer for 'System.Windows.Controls.TextBox' threw an exception. PropertyMetadata is already registered for type 'TextBox'.
这里是重现:
<StackPanel>
<TextBlock Text="123123" />
<TextBox Text="123123" BorderThickness="0" Padding="-2,0,-2,0" />
</StackPanel>
在 window 构造函数中(在 InitializeComponent()
之前)设置 TextBlock
字体是可行的。如何设置 TextBox
默认字体(我默认是 Segoe)?我需要一个解决方案来将它设置为 "Verdana"
在整个应用程序的一个地方。
智能感知显示:
对于您的整个应用程序,您可以在 App.xaml:
中进行设置
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="50"></Setter>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="100"></Setter>
</Style>
</Application.Resources>
对于单个文件,您可以在 Window 或 UserResource 开始标记后的 XAML 中进行设置:
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Window.Resources>
或者如果它是 'UserControl',请将 'Window' 替换为 'UserControl' - 你明白了。
您的基本格式如下:
<Window x:Class="WpfApplicationTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Text="123123" />
<TextBox Text="123123" BorderThickness="0" Padding="-2,0,-2,0" />
</StackPanel>
</Grid>
</Window>
祝你好运!
我的问题不是单独设置 TextBox
样式(抱歉误导了您),而是将其与 TextBlock
.
一起设置
问题似乎与这些控件没有可从中继承 FontFamilyProperty
的公共基础 class 有关。 TextBox
从 TextBoxBase
和 TextBlock
中取一个。尝试在后面的代码(window)或 window xaml 中设置两者将导致异常或无结果(对两者都不起作用)。
技巧 是在应用程序资源中设置它,不要问我为什么,但它确实有效(并且适用于所有内容):
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
<!-- not sure if this make sense -->
<Style TargetType="TextElement">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Application.Resources>
以下也有效(感谢 ):
// font overrides
TextElement.FontFamilyProperty.OverrideMetadata(typeof(TextElement),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
Control.FontFamilyProperty.OverrideMetadata(typeof(TextBoxBase),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
您可以将 TextBox
更改为 TextBoxBase
。不知何故它对我有用。
TextBoxBase.FontFamilyProperty.OverrideMetadata( typeof( TextBoxBase ),
new FrameworkPropertyMetadata( new FontFamily( "Verdana" ) ) );
如何设置 TextBox
的默认字体?
对于 TextBlock
它是(取自 here):
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
尝试对TextBox
做同样的事情:
TextBox.FontFamilyProperty.OverrideMetadata(typeof(TextBox),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
将抛出:
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: The type initializer for 'System.Windows.Controls.TextBox' threw an exception. PropertyMetadata is already registered for type 'TextBox'.
这里是重现:
<StackPanel>
<TextBlock Text="123123" />
<TextBox Text="123123" BorderThickness="0" Padding="-2,0,-2,0" />
</StackPanel>
在 window 构造函数中(在 InitializeComponent()
之前)设置 TextBlock
字体是可行的。如何设置 TextBox
默认字体(我默认是 Segoe)?我需要一个解决方案来将它设置为 "Verdana"
在整个应用程序的一个地方。
智能感知显示:
对于您的整个应用程序,您可以在 App.xaml:
中进行设置<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="50"></Setter>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="100"></Setter>
</Style>
</Application.Resources>
对于单个文件,您可以在 Window 或 UserResource 开始标记后的 XAML 中进行设置:
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Window.Resources>
或者如果它是 'UserControl',请将 'Window' 替换为 'UserControl' - 你明白了。
您的基本格式如下:
<Window x:Class="WpfApplicationTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Text="123123" />
<TextBox Text="123123" BorderThickness="0" Padding="-2,0,-2,0" />
</StackPanel>
</Grid>
</Window>
祝你好运!
我的问题不是单独设置 TextBox
样式(抱歉误导了您),而是将其与 TextBlock
.
问题似乎与这些控件没有可从中继承 FontFamilyProperty
的公共基础 class 有关。 TextBox
从 TextBoxBase
和 TextBlock
中取一个。尝试在后面的代码(window)或 window xaml 中设置两者将导致异常或无结果(对两者都不起作用)。
技巧 是在应用程序资源中设置它,不要问我为什么,但它确实有效(并且适用于所有内容):
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
<!-- not sure if this make sense -->
<Style TargetType="TextElement">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Application.Resources>
以下也有效(感谢
// font overrides
TextElement.FontFamilyProperty.OverrideMetadata(typeof(TextElement),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
Control.FontFamilyProperty.OverrideMetadata(typeof(TextBoxBase),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
您可以将 TextBox
更改为 TextBoxBase
。不知何故它对我有用。
TextBoxBase.FontFamilyProperty.OverrideMetadata( typeof( TextBoxBase ),
new FrameworkPropertyMetadata( new FontFamily( "Verdana" ) ) );