Reputation: 22008
How can I set default font for TextBox
?
For TextBlock
it's (taken from here):
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
Trying to do the same for TextBox
:
TextBox.FontFamilyProperty.OverrideMetadata(typeof(TextBox),
new FrameworkPropertyMetadata(new FontFamily("Verdana")));
will throw:
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'.
Here is repro:
<StackPanel>
<TextBlock Text="123123" />
<TextBox Text="123123" BorderThickness="0" Padding="-2,0,-2,0" />
</StackPanel>
Setting TextBlock
font as above in window constructor (before InitializeComponent()
) works. How to set TextBox
default font (it's Segoe by default to me)? I need a solution to set it in as "Verdana"
in one place for a whole application.
Intellisense shows:
Upvotes: 2
Views: 2182
Reputation: 22008
My problem is not setting TextBox
style alone (sorry for a missleading tittle), but setting it together with TextBlock
.
Problem seems related to that those controls don't have common base class to inherit FontFamilyProperty
from. TextBox
takes one from TextBoxBase
and TextBlock
from itself. Attempting to set both in either code behind (of window) or window xaml will lead to either exception or nothing (won't work for both).
The trick is to set it in application resources, don't ask me why, but it works then (and works for everything):
<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>
Following also works (thanks to @bars222's answer):
// 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")));
Upvotes: 0
Reputation: 1660
You can change TextBox
to TextBoxBase
. Somehow it worked for me.
TextBoxBase.FontFamilyProperty.OverrideMetadata( typeof( TextBoxBase ),
new FrameworkPropertyMetadata( new FontFamily( "Verdana" ) ) );
Upvotes: 2
Reputation: 408
For your entire application, you can set it in 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>
For individual files, you can set this in the XAML after your Window or UserResource opening tag:
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana" />
</Style>
</Window.Resources>
Or if it's a 'UserControl', replace 'Window' with 'UserControl' - you get the idea.
Your basic format would look like:
<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>
Good luck!
Upvotes: 1