Programmatically make a text block with a hyperlink between the text

In XAML, I have the following code:

<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> 

Now I want to get rid of the entire XAML text block and add this bit programmatically. I have nothing to create a TextBlock by setting the Text property to "click" please and adding a hyperlink to TextBlock.Content . But how to place a hyperlink between 'click' and "please"? And how to set the text of the hyperlink to "here"?

I can’t do much, until all I got is:

  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? 
+6
source share
1 answer

Here is the code to add a TextBlock with a clickable link in the middle:

 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); 
+11
source

Source: https://habr.com/ru/post/900043/


All Articles