locked
Hyperlink in WPF RRS feed

  • Question

  • I was trying to find out how to create a method which will give me a possibility to type a text and specify the link there and path,  to the file on the local machine (somefile.txt), and I'd like to ask you guys support me with my question.

    Example: You could download file from <here> , where <here> it's a local path: C:\\ProgramFiles.

    The following code needs to be improved in a way to add mentioned functionality.

          public void AppendMessage(TextBlock textBlock, string notifyMessage, SolidColorBrush foreground)
            {
                textBlock.Text = notifyMessage;
                textBlock.Foreground = foreground;
            }


    thank you in advance.
    Wednesday, October 21, 2009 3:18 PM

Answers

  • Here is solution:

    hyperLink.NavigateUri = new Uri(uri);


    public void AppendMessageLocalLink(TextBlock textBlock, string message, string linktext,string uri)
            {
                Hyperlink hyperLink = new Hyperlink(new Run(linktext));
                hyperLink.NavigateUri = new Uri(uri);
                textBlock.Inlines.Add(new Run(message + " "));
                textBlock.Inlines.Add(hyperLink);
            }


    Wednesday, October 21, 2009 5:36 PM

All replies

  • I'm not sure I understand your question. What's wrong with just using a System.Windows.Documents.Hyperlink, and handling the Click event? If you need to change the link at runtime you can either use a binding or modify the TextBlock.Inlines.
    Wednesday, October 21, 2009 4:21 PM
  • public void AppendMessageLocalLink(TextBlock textBlock, string message, string linktext)
            {
                Hyperlink hyperLink = new Hyperlink();
                hyperLink.Inlines.Add(linktext);
                textBlock.Inlines.Add(message + " ");
                textBlock.Inlines.Add(hyperLink);
            }
    I need to know how to assign word to the hyperlink, to display "word" which is a hyperlink !
    Wednesday, October 21, 2009 4:26 PM
  • Put a Run inside the Hyperlink:

    public void AppendMessageLocalLink(TextBlock textBlock, string message, string linktext)
            {
                Hyperlink hyperLink = new Hyperlink(new Run(linktext));
                textBlock.Inlines.Add(new Run(message + " "));
                textBlock.Inlines.Add(hyperLink);
            }

    • Proposed as answer by J_________ Wednesday, October 21, 2009 4:41 PM
    Wednesday, October 21, 2009 4:41 PM
  • but it's still displaying hyperlink as www.msdn.com, I need to have msdn as a hyperlink.

    thanks
    Wednesday, October 21, 2009 4:52 PM
  • Here is solution:

    hyperLink.NavigateUri = new Uri(uri);


    public void AppendMessageLocalLink(TextBlock textBlock, string message, string linktext,string uri)
            {
                Hyperlink hyperLink = new Hyperlink(new Run(linktext));
                hyperLink.NavigateUri = new Uri(uri);
                textBlock.Inlines.Add(new Run(message + " "));
                textBlock.Inlines.Add(hyperLink);
            }


    Wednesday, October 21, 2009 5:36 PM