views:

49

answers:

2

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?

+3  A: 

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"));
dtb
That works fine. It does seem to have a problem with not displaying the underline if it's under a space character with no non-space characters following but I've fixed that by always having some text at the end: pragmatism over purity :-) Thanks heaps.
paxdiablo
+1  A: 

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

Ray Henry
Thanks for the links, Ray. I didn't actually know this was possible (the previous incarnation of the application used Java's inline HTML). Now that I know about runs, it'll be a lot easier in future.
paxdiablo