Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.32 KB  |  hits: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to set the links in a text block clickable in wp7
  2. <TextBlock>
  3.     <Run>Pure Text</Run>
  4.     <HyperLink Command="{Binding HyperLinkTapped}">http://google.com</HyperLink>
  5.     <Run>Pure Text Again</Run>
  6. </TextBlock>
  7.        
  8. private void OnMessageReceived(string message)
  9. {
  10.     var textBlock = new RichTextBox()
  11.     {
  12.         TextWrapping = TextWrapping.Wrap,
  13.         IsReadOnly = true,
  14.     };
  15.  
  16.     var paragraph = new Paragraph();
  17.  
  18.     var runs = new List<Inline>();
  19.  
  20.     foreach (var word in message.Split(' '))
  21.     {
  22.         Uri uri;
  23.  
  24.         if (Uri.TryCreate(word, UriKind.Absolute, out uri) ||
  25.            (word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri)))
  26.         {
  27.             var link = new Hyperlink();
  28.             link.Inlines.Add(new Run() { Text = word });
  29.             link.Click += (sender, e) =>
  30.             {
  31.                 var hyperLink = (sender as Hyperlink);
  32.                 new WebBrowserTask() { Uri = uri }.Show();
  33.             };
  34.  
  35.             runs.Add(link);
  36.         }
  37.         else
  38.         {
  39.             runs.Add(new Run() { Text = word });
  40.         }
  41.  
  42.         runs.Add(new Run() { Text = " "});
  43.     }
  44.  
  45.     foreach (var run in runs)
  46.         paragraph.Inlines.Add(run);
  47.  
  48.     textBlock.Blocks.Add(paragraph);
  49.  
  50.     MessagesListBox.Children.Add(textBlock);
  51.     MessagesListBox.UpdateLayout();
  52. }