这是 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
元素。它只是不像你所期望的那样工作。