16

在我的 WPF 应用程序中,我有这个:

<StackPanel>
  <TextBlock>
     <Hyperlink>
       <TextBlock TextWrapping="Wrap" Name="HyperlinkText" />
     </Hyperlink>
  </TextBlock>
</StackPanel>

但是,如果我设置HyperlinkText.Text为一个长文本,则整个文本在底部只加下划线一次(见图)。有没有办法让每一行单独加下划线而不需要手动换行?

4

3 回答 3

17

这是 WPF 中一个非常非常烦人的问题。我什至称它为错误。

正如@levanovd 在他的回答中提到的那样,您可以通过使用 aRun作为内部元素来获得正确包装的超链接:

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

这很好用,直到您想超链接中应用文本格式。如果您尝试这样做,例如:

    <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>

你会得到一个编译错误:

对象“运行”已经有一个孩子,不能添加“”。'Run' 只能接受一个孩子。

因此,正如@Scott Whitlock 所指出的,您必须使用 aTextBlock作为内部元素并使用 and 的TextDecoration属性来Hyperlink代替TextBlock

    <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>

叹。我真的很讨厌 WPF 的Hyperlink元素。它只是不像你所期望的那样工作。

于 2010-09-17T01:35:35.120 回答
9

实现这一点的更简单方法是使用RunTextBlock 代替。

希望能帮助到你。

于 2009-11-25T19:57:57.843 回答
2

尝试更改超链接的样式以删除下划线。然后为内部 TextBlock 样式本身添加下划线。

于 2009-08-12T15:07:22.697 回答