16

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?

svick
  • 236,525
  • 50
  • 385
  • 514

3 Answers3

17

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:

    <StackPanel>
        <TextBlock TextWrapping="Wrap">
            <Hyperlink><Run>This is a really long hyperlink. Yeah, a really really long hyperlink, whaddaya think?</Run></Hyperlink>
        </TextBlock>
    </StackPanel>

This works great, until you want to apply text formatting within the hyperlink. If you tried to do this, for example:

    <StackPanel>
        <TextBlock TextWrapping="Wrap">
            <Hyperlink><Run>This is a really long <Run TextWeight="Bold">hyperlink</Run>. Yeah, a really really long hyperlink, whaddaya think?</Run></Hyperlink>
        </TextBlock>
    </StackPanel>

You'd get a compile error:

The object 'Run' already has a child and cannot add ''. 'Run' can accept only one child.

So, as @Scott Whitlock noted, you have to use a TextBlock as the inner element and mess around with the TextDecoration attributes of the Hyperlink and TextBlock instead:

    <StackPanel>
        <TextBlock>
            <Hyperlink TextDecorations="None"><TextBlock TextWrapping="Wrap" TextDecorations="Underline">This is a really long <Run FontWeight="Bold">hyperlink</Run>. Yeah, a really really long hyperlink, whaddaya think?</TextBlock></Hyperlink>
        </TextBlock>
    </StackPanel>

Sigh. I really hate WPF's Hyperlink element. It just doesn't work anything like you'd expect.

dthrasher
  • 40,656
  • 34
  • 113
  • 139
9

An easier way to achieve that is to use Run instead of TextBlock.

Hope it helps.

Rachel Martin
  • 525
  • 6
  • 18
levanovd
  • 4,095
  • 6
  • 37
  • 56
2

Try changing the style of the Hyperlink to remove the underline. Then add an underline to the inner TextBlock style itself.

Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
  • Care to post some code? I would like to try this but not sure how to remove the underline without breaking the rest of the style. And I'm lazy :-) – stone Sep 02 '10 at 23:30
  • @skypecakes: levanovd's answer is better. See this for more info: http://www.longhorncorner.com/UploadFile/raj1979/TextWPF01062009041311AM/TextWPF.aspx – Scott Whitlock Sep 03 '10 at 01:43
  • Thanks, Scott. That looked promising, but unfortunately won't work for me because I am populating the text with a data binding and on .Net 3.5. You can't bind Run.Text in 3.5. I am less lazy today so I'll see what I can come up with and post the results as an answer to the OP's question, if it's good enough. – stone Sep 07 '10 at 18:34
  • @skypecakes: There are lots of attached property examples out there that you can use to bind non-bindable properties directly in xaml. – Scott Whitlock Sep 08 '10 at 12:22