In my WPF application I have this:
<StackPanel>
<TextBlock>
<Hyperlink>
<TextBlock TextWrapping="Wrap" Name="HyperlinkText" />
</Hyperlink>
</TextBlock>
</StackPanel>
But if I set HyperlinkText.Text
to a long text that wraps, the whole text is underlined only once at the bottom (see image). Is there a way to have every line underlined separately without manual wrapping?
This is a really, really annoying problem in WPF. I'd go so far as to call it a bug.
As @levanovd mentioned in his answer, you can get a hyperlink to wrap properly by using a
Run
as the inner element:This works great, until you want to apply text formatting within the hyperlink. If you tried to do this, for example:
You'd get a compile error:
So, as @Scott Whitlock noted, you have to use a
TextBlock
as the inner element and mess around with theTextDecoration
attributes of theHyperlink
andTextBlock
instead:Sigh. I really hate WPF's
Hyperlink
element. It just doesn't work anything like you'd expect.Try changing the style of the Hyperlink to remove the underline. Then add an underline to the inner TextBlock style itself.
An easier way to achieve that is to use Run instead of TextBlock.
Hope it helps.