在我的 WPF 应用程序中,我有这个:
<StackPanel>
<TextBlock>
<Hyperlink>
<TextBlock TextWrapping="Wrap" Name="HyperlinkText" />
</Hyperlink>
</TextBlock>
</StackPanel>
但是如果我设置
HyperlinkText.Text
对于换行的长文本,整个文本在底部只加一次下划线(见图)。有没有办法让每一行单独加下划线而不需要手动换行?最佳答案
这是 WPF 中一个非常非常烦人的问题。我什至称其为错误。
正如@levanovd 在他的回答中提到的,您可以通过使用 Run
获得一个正确包装的超链接。作为内部元素:
<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>
你会得到一个编译错误:
The object 'Run' already has a child and cannot add ''. 'Run' can accept only one child.
因此,正如@Scott Whitlock 所指出的,您必须使用
TextBlock
作为内部元素并乱搞 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
元素。它只是没有像你期望的那样工作。
关于wpf - WPF 超链接中的文本换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1266668/