Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a System.Drawing.Font with a WPF Label

I have a WPF Label control which I'm trying to change the appearance of using a System.Drawing.Font object supplied by some legacy code. I have been able to set most of the properties, but am struggling with Strikeout and Underline.

So far I have:

System.Drawing.Font font = FontFromLegacyCode();

System.Windows.Controls.Label label = new System.Windows.Controls.Label();
label.FontFamily = new System.Windows.Media.FontFamily( font.Name );
label.FontWeight = font.Bold ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Regular;
label.FontStyle = font.Italic ? System.Windows.FontStyles.Italic : System.Windows.FontStyles.Normal;
label.FontSize = font.Size;

How do you set the font strikeout or underline properties? Is there a better control to use?

like image 391
bstoney Avatar asked Mar 12 '09 07:03

bstoney


People also ask

What are custom fonts in WPF?

Custom fonts can be specified in WPF C# windows applications for different controls by defining custom fonts in styles. These custom fonts can be True Type fonts (.ttf) or OpenType fonts (.otf).

What is WPF label?

WPF - Label The Label class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls.

How to add two fonts to a WPF project?

In your WPF Project folder, add new folder named fonts and add any two fonts you wish. I’m using Moon.otf and Bradley Hand ITC (bradhitc.ttf) fonts.

Is there a one to one mapping between WinForms and WPF fonts?

There is no one to one mapping between System.Drawing.Font (WinForms) and System.Windows.Media.FontFamily (WPF). Have a look at the FontFamily class here [ ^] and try using it in your own implementation.


2 Answers

I would change it to a TextBlock control. The TextBlock control has the TextDecorations property you can use.

<TextBlock Name="textBlock" TextDecorations="Underline, Strikethrough" />

Or you can stick a TextBlock inside a Label if you really like (although I'd just use the TextBlock by itself).

<Label Name="label">
    <TextBlock Name="textBlock" TextDecorations="Underline, Strikethrough" />
</Label>

Have a look at the TextDecorations class.

I find that TextBlocks are more suitable than Labels in most situations. Here's a blog post about the differences. The chief difference being that a Label is a Control whereas a TextBlock is just a FrameworkElement. Also a Label supports access keys.

like image 61
Ray Avatar answered Nov 19 '22 04:11

Ray


Looking at the code you already have, there might be a problem with it. On the MSDN on Windows Form and WPF Property mapping they make the comment:

Font size in WPF is expressed as one ninety-sixth of an inch, and in Windows Forms as one seventy-second of an inch. The corresponding conversion is:

Windows Forms font size = WPF font size * 72.0 / 96.0.

like image 21
Erik Allen Avatar answered Nov 19 '22 05:11

Erik Allen