Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font family programmatically in wpf

I'm creating a WPF application.

I have created a folder in my solution and I have some fonts in it.

How can I change programmatically the TextBlock FontFamily ?

like image 916
omid naghipoor Avatar asked Feb 04 '18 15:02

omid naghipoor


People also ask

How do I change the font family in XAML?

In XAML, the usage is <object TextBlock. FontFamily="value".../> , where object is an object element (typically a flow element) contained within a TextBlock, and value is one of the string-format values as explained in XAML Values. In code, the attached property usage is supported by GetFontFamily and SetFontFamily.

How do I change a style in WPF?

From MSDN: Once a style has been applied, it is sealed and cannot be changed. If you want to dynamically change a style that has already been applied, you must create a new style to replace the existing one. and you think this line Application.

What is font family in WPF?

In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units.

How do you use font family?

Start with the font you want, and always end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available. Note: Separate each value with a comma. Note: If a font name contains white-space, it must be quoted.


1 Answers

XAML

<TextBlock 
  Name="textBlock"

  Background="AntiqueWhite" 
  Foreground="Navy" 

  FontFamily="Century Gothic"
  FontSize="12"
  FontStretch="UltraExpanded"
  FontStyle="Italic"
  FontWeight="UltraBold"

  LineHeight="Auto"
  Padding="5,10,5,10"
  TextAlignment="Center"
  TextWrapping="Wrap"

  Typography.NumeralStyle="OldStyle"
  Typography.SlashedZero="True"
>
  <Run Background="LightGreen">Text run 1.</Run>
  <LineBreak/><Run Background="LightBlue">Text run 2.</Run>
  <LineBreak/><Run Background="LightYellow">Text run 3.</Run>
</TextBlock>

Code behind

TextBlock textBlock = new TextBlock(new Run("A bit of text content..."));

textBlock.Background              = Brushes.AntiqueWhite;
textBlock.Foreground              = Brushes.Navy;

textBlock.FontFamily              = new FontFamily("Century Gothic");
textBlock.FontSize                = 12;
textBlock.FontStretch             = FontStretches.UltraExpanded;
textBlock.FontStyle               = FontStyles.Italic;
textBlock.FontWeight              = FontWeights.UltraBold;

textBlock.LineHeight              = Double.NaN;
textBlock.Padding                 = new Thickness(5, 10, 5, 10);
textBlock.TextAlignment           = TextAlignment.Center;
textBlock.TextWrapping            = TextWrapping.Wrap;

textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle;
textBlock.Typography.SlashedZero  = true;

if you want to load a custom font

controlID.FontFamily = new FontFamily("file:///Font
 Full Path");
like image 159
Lion King Avatar answered Sep 20 '22 11:09

Lion King