2

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:

TextBlock

TextBox

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Calling OverrideMetadata for a type of the framework (e.g. TextBlock) is dubious anyway. You should set default values by Styles. – Clemens Apr 04 '16 at 13:06
  • I tried setting `TextElement` it doesn't work for anything. Setting `TextBlock` one works, but only for `TextBlock`. – Sinatr Apr 04 '16 at 13:08
  • Have you tried `OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey)` version with `DependencyPropertyKey` – bars222 Apr 04 '16 at 13:09
  • @bars222, no idea what you mean. Can you write a complete line of code? – Sinatr Apr 04 '16 at 13:10
  • @Clemens, how do I set style which applies to either (`TextBox` or `TextBlock`) same font? See [@Kiel's answer](http://stackoverflow.com/a/36403596/1997232), it works, but only for one (never for both). They have `FrameworkElement` base class, but it doesn't contains `FontFamilyProperty`. – Sinatr Apr 04 '16 at 13:15

3 Answers3

2

You can change TextBox to TextBoxBase. Somehow it worked for me.

TextBoxBase.FontFamilyProperty.OverrideMetadata( typeof( TextBoxBase ),
                new FrameworkPropertyMetadata( new FontFamily( "Verdana" ) ) );
bars222
  • 1,652
  • 3
  • 13
  • 14
  • VS15 suggest to use `Control. ... typeof(TextBoxBase)`. Strangely enough that combo works (and I like it more than `app.xaml` solution), thanks a lot, wouldn't guess myself ever. – Sinatr Apr 04 '16 at 13:38
  • You're welcome. Thanks for the way to changing fonts in app (for useful question). – bars222 Apr 04 '16 at 13:41
1

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!

Kiel
  • 408
  • 4
  • 13
  • I have 20 windows and 50 user controls, it would be good to set defaults in one place. Your example works **only** for `TextBlock` (`TextBox` font style is still "Segoe"). I can change `TargetType` to `TextBox` and then it will only work for `TextBox`. Setting both seem to only apply style to the first defined one. Funny. – Sinatr Apr 04 '16 at 13:09
  • @Sinatr - you could set it globally in App.xaml. – Kiel Apr 04 '16 at 13:16
  • Also, the FontSize aimed at TextBlock also affects Buttons (at runtime, not in Studio)...?!? – T4NK3R Aug 27 '16 at 10:50
0

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")));
Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319