Greetings, I have some text in a db and it is as follows:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tellus nisl, venenatis et pharetra ac, tempor sed sapien. Integer pellentesque blandit velit, in tempus urna semper sit amet. Duis mollis, libero ut consectetur interdum, massa tellus posuere nisi, eu aliquet elit lacus nec erat. Praesent a commodo quam. **[a href='http://somesite.com']some site[/a]**Suspendisse at nisi sit amet massa molestie gravida feugiat ac sem. Phasellus ac mauris ipsum, vel auctor odio
My question is: How can I display a Hyperlink
in a TextBlock
? I don't want to use a webBrowser control for this purpose.
I don't want to use this control either: http://www.codeproject.com/KB/WPF/htmltextblock.aspx also
You can use Regex with a value converter in such situation.
Use this for your requirements (original idea from here):
This will match all links in your string containing links, and make 2 named groups for each match :
link
andtext
Now you can iterate through all the matches. Each match will give you a
Note : use this logic in your custom
ConvertToHyperlinkedText
value converter.Displaying is rather simple, the navigation is another question. XAML goes like this:
And the event handler that launches default browser to navigate to your hyperlink would be:
Edit: To do it with the text you've got from database you'll have to parse the text somehow. Once you know the textual parts and hyperlinked parts, you can build textblock contents dynamically in the code: