Created
December 9, 2014 13:55
-
-
Save Tapanila/ad7d15f33d47eb081a06 to your computer and use it in GitHub Desktop.
BindableRichTextbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Documents; | |
using System.Windows.Media; | |
namespace Slush.Helpers | |
{ | |
public class BindableRichTextBox : RichTextBox | |
{ | |
private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"; | |
public static readonly DependencyProperty TextProperty = | |
DependencyProperty.Register("Text", typeof(string), typeof(BindableRichTextBox), new PropertyMetadata(default(string), TextPropertyChanged)); | |
public static readonly DependencyProperty HyperLinkForegroundProperty = | |
DependencyProperty.Register("HyperLinkForegroundProperty", typeof (Brush), typeof(BindableRichTextBox), | |
new PropertyMetadata(default(Brush))); | |
public string Text | |
{ | |
get { return (string)GetValue(TextProperty); } | |
set { SetValue(TextProperty, value); } | |
} | |
public Brush HyperLinkForeground | |
{ | |
get { return (Brush) GetValue(HyperLinkForegroundProperty); } | |
set { SetValue(HyperLinkForegroundProperty, value);} | |
} | |
private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) | |
{ | |
var richTextBox = (BindableRichTextBox)dependencyObject; | |
richTextBox.Blocks.Clear(); | |
var text = (string)dependencyPropertyChangedEventArgs.NewValue; | |
var textPosition = 0; | |
var paragraph = new Paragraph(); | |
var urlMatches = Regex.Matches(text, UrlPattern); | |
foreach (Match urlMatch in urlMatches) | |
{ | |
var urlOccurrenceIndex = text.IndexOf(urlMatch.Value, textPosition, StringComparison.Ordinal); | |
if (urlOccurrenceIndex == 0) | |
{ | |
var hyperlink = new Hyperlink | |
{ | |
NavigateUri = new Uri(urlMatch.Value), | |
TargetName = "_blank", | |
Foreground = richTextBox.HyperLinkForeground | |
}; | |
hyperlink.Inlines.Add(urlMatch.Value); | |
paragraph.Inlines.Add(hyperlink); | |
textPosition += urlMatch.Value.Length; | |
} | |
else | |
{ | |
paragraph.Inlines.Add(text.Substring(textPosition, urlOccurrenceIndex - textPosition)); | |
textPosition += urlOccurrenceIndex - textPosition; | |
var hyperlink = new Hyperlink | |
{ | |
NavigateUri = new Uri(urlMatch.Value), | |
TargetName = "_blank", | |
Foreground = richTextBox.HyperLinkForeground | |
}; | |
hyperlink.Inlines.Add(urlMatch.Value); | |
paragraph.Inlines.Add(hyperlink); | |
textPosition += urlMatch.Value.Length; | |
} | |
} | |
if (urlMatches.Count == 0) | |
{ | |
paragraph.Inlines.Add(text); | |
} | |
richTextBox.Blocks.Add(paragraph); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment