Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to underline one character in a WPF TextBlock?

Is there a way to apply an underline text decoration to one character only in a TextBlock (or any amount less than the full block)?

I have some text that I want output as "this worf is misspelt" and have the f in worf underlined.

I know you can do:

TextBlock47.TextDecorations = TextDecorations.Underline;

but I don't want the entire block underlined.

Failing that, is there another control I can use other than TextBlock that gives this capability? I've looked into rich text but that seems like an awful lot of work for what's a simple effect. If that is the only way, how do I go about generating text of a specific format (10pt, Courier New, one character underlined) in c# code?

like image 208
paxdiablo Avatar asked Oct 08 '10 03:10

paxdiablo


People also ask

Is TextBlock editable in WPF?

TextBlock is not editable.

How do I make text bold in WPF?

textBlock. Inlines. Add(new Run(Boldsplist) { FontWeight = FontWeights. Bold }); this.

How do you underline a label in HTML?

HTML <u> Tag. The <u> tag in HTML stands for underline, and it's used to underline the text enclosed within the <u> tag. This tag is generally used to underline misspelled words.

What is TextBlock?

textblock (plural textblocks) The block of pages making up a book, excluding the binding.


2 Answers

You can use Underline in a TextBlock:

<TextBlock Name="textBlock47">
  this wor<Underline>f</Underline> is misspelt
</TextBlock>

or

textBlock47.Inlines.Add(new Run("this wor"));
textBlock47.Inlines.Add(new Underline(new Run("f")));
textBlock47.Inlines.Add(new Run(" is misspelt"));
like image 162
dtb Avatar answered Oct 24 '22 17:10

dtb


Did you look at the Run tag?

http://www.codeproject.com/Tips/60784/WPF-RichTextBox-features-in-TextBlock.aspx

http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx

like image 23
Ray Henry Avatar answered Oct 24 '22 17:10

Ray Henry