1

I've got an application that shows a list of messages that are shown as a TextBox:

<TextBox Text="{Binding ., Converter={StaticResource MessageTextConverter}}"
                           BorderThickness="0" 
                           Background="Transparent" 
                           IsReadOnly="True" 
                           FontSize="14" 
                           TextWrapping="Wrap" 
                           VerticalAlignment="Stretch"/>

Those messages may or may not contain a link or an email. So what I needed was a way to dynamically check the text for any links and emails and set the Hyperlinks in the TextBox.

I searched online and could not find a similar case. Which is the best way to do this? Using a regex?

EDIT:

Some people marked this as duplicated of C# WPF Text with links , but this question is about showing a hyperlink and making it clickable. I already knew about this and how to use it. My question is, what is the best way to check a random text for a link or an email and mark it.

Example of entries o text:

Entry 1: "Some text before url http://www.google.com"
Entry 2: "http://www.google.com some text that has nothing to do with url"
Entry 3: "Some text before url http://www.google.com some text after url"
Entry 4: "Some text before urls http://www.google.com some text between urls http://www.google.com some text after urls"

In resume, given a random text that may contain an url in the middle, what is the best way to separate that chunk of url and highlight it? Using a regex? Or is there another way to do this in wpf that is easier?

Community
  • 1
  • 1
Daniel
  • 639
  • 8
  • 24
  • 1
    You cannot add a HyperLink in TextBox. You need to use RichTextBox. – Ayyappan Subramanian Apr 28 '15 at 20:56
  • If you only want to show text with hyperlinks, you may add `Run` and [`Hyperlink`](https://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx) elements to the Inlines collection of a `TextBlock`, not a `TextBox`. – Clemens Apr 28 '15 at 21:00
  • I want to show the entire message even if it has no hyperlinks. I just wanted to highlight the links that may appear in the middle of the message – Daniel Apr 28 '15 at 21:43
  • 1
    possible duplicate of [C# WPF Text with links](http://stackoverflow.com/questions/3414426/c-sharp-wpf-text-with-links) – ΩmegaMan Apr 28 '15 at 21:48
  • It's a different problem, editted the question – Daniel Apr 29 '15 at 02:31

0 Answers0