在 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,将 Text 属性设置为“请单击”并将超链接添加到 TextBlock.Content
。但是如何将超链接定位在“点击”和“请”之间?以及如何将超链接的文本设置为“此处”?
我没有太多进展,到目前为止我得到的是:
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);
关于c# - 以编程方式在文本之间制作带有超链接的文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890159/