以编程方式使文本块与文本之间的超链接
本文关键字:文本 之间 超链接 编程 方式使 | 更新日期: 2023-09-27 18:12:15
在XAML中,我有以下代码:
<Label Width="120" Height="20" Name="label1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Bottom">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left">
click
<Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="foo">here</Hyperlink>
please
</TextBlock>
</Label>
现在我想摆脱整个TextBlock XAML并以编程方式添加该位。我没有麻烦创建TextBlock,设置文本属性为"点击请",并添加一个超链接到TextBlock.Content
。但是我如何将超链接定位在"点击"answers"请"之间呢?我如何将超链接的文本设置为"这里"?
我没有做太多的事情,到目前为止我只得到这个:
label2.Content = new TextBlock() { Text = "click please" };
//(label2.Content as TextBlock).Content does not exist?
//and even if it does.. how do I squeeze the hyperlink in between the text?
下面是在中间添加一个可点击链接的TextBlock
的代码:
Run run1 = new Run("click ");
Run run2 = new Run(" Please");
Run run3 = new Run("here.");
Hyperlink hyperlink = new Hyperlink(run3)
{
NavigateUri = new Uri("http://stackoverflow.com")
};
hyperlink.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(hyperlink_RequestNavigate); //to be implemented
textBlock1.Inlines.Clear();
textBlock1.Inlines.Add(run1);
textBlock1.Inlines.Add(hyperlink);
textBlock1.Inlines.Add(run2);