Search code examples
c#wpfvisual-studio-2008hyperlinktextblock

Textblock with link to text file inside


I am setting different text to a textblock depending on what control has been selected as a way of providing help for the user.

What I would like to do is in the code behind file, when one control is selected, provide a brief explanation in text, then provide a link to a text file within that textblock.

It might look like, for example "Your choice should be a car manufacturer. Click here to see a list"

I was trying to do it with a hyperlink but i'm not having much luck.

Anyone know how to do it?


Solution

  • Use the TextBlock.Inlines collection and add a Hyperlink:

    XAML:

    <TextBlock Name="hintInfo" />
    

    Code:

    Hyperlink hlink = new Hyperlink(new Run("here"));
    hlink.Click += SomeEventHandler;  // event handler to open text file
    
    hintInfo.Inlines.Clear();
    hintInfo.Inlines.Add("Click ");
    hintInfo.Inlines.Add(hlink);
    hintInfo.Inlines.Add(" to see more info.");
    

    To display the text file, you could use Process.Start to launch an external viewer (e.g. Notepad), or you could use File.ReadAllText to read it in, and then display it in a TextBlock or whatever within your app.