5

I Want to set the font of my WPF dialog to similar of an existing Winforms dialog i.e. Microsoft Sans Serif, 8.25pt. How can we specify the Font unit in WPF?

Make It Perfect
  • 935
  • 2
  • 10
  • 20

3 Answers3

7

Just include the text pt or px in the font-size. For example,

<TextBlock Width="400" Text="">
   <Run FontFamily="Microsoft Sans Serif"
        FontSize="8.25 pt">Hello world!</Run>
</TextBlock>

1 pt is defined as 1/72nd of an inch; 1 px is 1/96th of an inch.

Note that unit qualifiers are only supported in WPF, not in Silverlight.

There is no equivalent to the CSS unit em, but there is a workaround.

Community
  • 1
  • 1
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • 1
    No, one `pt` is 1/72 of an inch just like everywhere else. You're thinking of WPF "pixels", which are 1/96 of an inch. – Joe White Apr 19 '11 at 12:30
2

Here is a MSDN article on WPF font sizes;

http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.fontsize(v=vs.95).aspx

Craig White
  • 13,492
  • 4
  • 23
  • 36
0

You can acheive that by setting the FontFamily and the FontSize properties of the control.

eg.

<UserControl x:Class="FitPredictionModule.Views.BondTestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             FontFamily="Microsoft Sans Serif"
             FontSize="8.25 pt">
             <!-- Content here -->
</UserControl>

You can see that I have set the FontFamily to Microsoft Sans Serif and the FontSize to 8.25 pt. Know that Windows Forms font size = WPF font size * 72.0 / 96.0.

Jean-Louis Mbaka
  • 1,604
  • 11
  • 16
  • You're saying Windows Forms FontSize is in Pixels only? And that WPF is in Points only? That's not right at all. -- In both WinForms and in WPF you can specify if you want to measure your font sizes in Pixels or in Points. (However, the formula you give for converting from points to pixels is clearly correct.) – BrainSlugs83 Jan 04 '14 at 05:54