Search code examples
c#xamlwindows-phone-8hyperlink

Adding hyperlink button in a text block programatically


How can i add a hyperlink button inside a text block through c# at the end of the text. In XAML part i have a text block

<TextBlock  Name="PresenterDescription" TextWrapping="Wrap"  Foreground="White" MinHeight="200" FontSize="16"/>

in .cs file i am doing this..

Run run1 = new Run();
run1.Text = "some text";
HyperlinkButton hyperlinkButton = new HyperlinkButton()
{
    Content = " read more..",
    HorizontalAlignment = HorizontalAlignment.Left,
    NavigateUri = new Uri("http://somelink.com", UriKind.Absolute)
};
PresenterDescription.Inlines.Add(run1);

But how can i add the hyperlink button to this text block ?, As i can not add it as inline..


Solution

  • In the XAML part i did the following

    <RichTextBox  Name="PresenterDescription"  VerticalAlignment="Top" FontSize="16">
                <Paragraph >
                           <Hyperlink Click="readMoreclick">
                                <Underline Foreground="White">read more..</Underline>
                          </Hyperlink>
                 </Paragraph>
     </RichTextBox>
    

    And in c# part the text to be displayed before the link as..

    PresenterDescription.Selection.Text = "Text to be displayed";
    

    And finally the event handler for link click..

      private void readMoreclick(object sender, RoutedEventArgs e)
             {
                WebBrowserTask webBrowserTask = new WebBrowserTask();
                webBrowserTask.Uri = new Uri("www.example.com", UriKind.Absolute);
                webBrowserTask.Show();
             }