我在LocalizedResources
绑定文本(针对各种语言),我需要在该文本中包含链接。不幸的是,当我像这样绑定它时,我无法在其中包含链接:
<TextBlock TextWrapping="Wrap" Text="{Binding Path=LocalizedResources.AboutText, Source={StaticResource LocalizedStrings}}"/>
所以我基本上想在那个文本链接块中创建一些单词。这可能吗?
编辑:我需要这样的东西除非我正如我所说的那样绑定来自LocalizedResources
的文本
答案 0 :(得分:3)
建议Romasz,您应该使用RichTextBox而不是TextBlock。您可以使用以下代码解决绑定:
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
对于超链接,您可以使用C#浏览整个文本,然后将链接与文本分开(您可以使用类似的东西,而且您知道每当文本中出现一个---时,您就有了超链接)。
获得文本和超链接后,可以使用以下代码将它们添加到RichTextBox:
Run myRun = new Run();
myRun.Text = "Displaying text with ";
Hyperlink link = new Hyperlink();
link.Inlines.Add("hyperlink");
link.NavigateUri = new Uri("https://stackoverflow.com/");
link.TargetName = "_blank";
link.Foreground = new SolidColorBrush(Colors.Blue);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(link);
myRun = new Run();
myRun.Text = " and with some text after the link.";
myParagraph.Inlines.Add(myRun);
rtb.Blocks.Add(myParagraph);
其中rtb是我的RichTextBox的名称。
答案 1 :(得分:0)
直截了当的解决方案如下: 将文本拆分为几个块,例如
。在链接1之前 。链接1 。在链接2之前 。链接2 。链接2之后 为每个前/后链接部分创建文本块,并为链接添加超链接按钮。
您应该能够将这些部分混合在一起,以便用户不会区分文本和链接
希望这有帮助
这就是代码:
<TextBlock FontSize="20">
<Run x:Uid="one" />
<Run x:Uid="link_description" Foreground="BlueViolet">
<iact:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:OpenHyperlinkAction x:Uid="link" />
</core:EventTriggerBehavior>
</iact:Interaction.Behaviors>
</Run>
<Run x:Uid="two" />
</TextBlock>
或者:
<TextBlock FontSize="20">
<Run x:Uid="one" />
<Hyperlink x:Uid="link" Foreground="BlueViolet" >
<Run x:Uid="link_description" />
</Hyperlink>
<Run x:Uid="two" />
</TextBlock>