locked
Bindable RichtextBox RRS feed

  • Question

  • I need to bind text which may contain hyperlinks to RichTextBox so it could show text as normal text and links as hyperlinks.

    For example I have following text:

    Join us on social networks
    http://www.facebook.com/
    

    I want that links in a text be hyperlinks so the result in RichTextBox would be like this:

    Join us on social networks

    http://www.facebook.com/


    Thursday, December 20, 2012 6:49 AM

Answers

  • I have created a class BindableRichTextBox which inherits the RichTextBox.  It converts the binded string into a paragraph and adds it to the RichTextBox.

    Im not able to use the created BindableRichTextBox.

    Please help me how can i use this BindableRichTextBox in my XAML file.

    The code is below.

     public class BindableRichTextBox : RichTextBox
        {
            private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";

            public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(BindableRichTextBox), new PropertyMetadata(default(string), TextPropertyChanged));

            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

            private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
            {
                var richTextBox = (BindableRichTextBox)dependencyObject;
                var text = (string)dependencyPropertyChangedEventArgs.NewValue;
                int textPosition = 0;
                var paragraph = new Paragraph();

                var urlMatches = Regex.Matches(text, UrlPattern);
                foreach (Match urlMatch in urlMatches)
                {
                    int urlOccurrenceIndex = text.IndexOf(urlMatch.Value, textPosition, StringComparison.Ordinal);

                    if (urlOccurrenceIndex == 0)
                    {
                        var hyperlink = new Hyperlink
                                            {
                                                NavigateUri = new Uri(urlMatch.Value),
                                                TargetName = "_blank",
                                                Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                            };
                        hyperlink.Inlines.Add(urlMatch.Value);
                        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 = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                            };
                        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);
            }
        }

    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:35 AM
    • Unmarked as answer by jaintanya1128 Thursday, December 20, 2012 9:36 AM
    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:41 AM
    Thursday, December 20, 2012 6:56 AM
  • I found the way of using the bindable Richtextbox in my xaml.

    Just had to add - xmlns:local ="clr-namespace:SocialApp" 

    and <local:BindableRichTextBox Text="Bindable Details"/>

    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:41 AM
    Thursday, December 20, 2012 9:41 AM

All replies

  • I have created a class BindableRichTextBox which inherits the RichTextBox.  It converts the binded string into a paragraph and adds it to the RichTextBox.

    Im not able to use the created BindableRichTextBox.

    Please help me how can i use this BindableRichTextBox in my XAML file.

    The code is below.

     public class BindableRichTextBox : RichTextBox
        {
            private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?";

            public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(BindableRichTextBox), new PropertyMetadata(default(string), TextPropertyChanged));

            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

            private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
            {
                var richTextBox = (BindableRichTextBox)dependencyObject;
                var text = (string)dependencyPropertyChangedEventArgs.NewValue;
                int textPosition = 0;
                var paragraph = new Paragraph();

                var urlMatches = Regex.Matches(text, UrlPattern);
                foreach (Match urlMatch in urlMatches)
                {
                    int urlOccurrenceIndex = text.IndexOf(urlMatch.Value, textPosition, StringComparison.Ordinal);

                    if (urlOccurrenceIndex == 0)
                    {
                        var hyperlink = new Hyperlink
                                            {
                                                NavigateUri = new Uri(urlMatch.Value),
                                                TargetName = "_blank",
                                                Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                            };
                        hyperlink.Inlines.Add(urlMatch.Value);
                        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 = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                            };
                        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);
            }
        }

    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:35 AM
    • Unmarked as answer by jaintanya1128 Thursday, December 20, 2012 9:36 AM
    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:41 AM
    Thursday, December 20, 2012 6:56 AM
  • I found the way of using the bindable Richtextbox in my xaml.

    Just had to add - xmlns:local ="clr-namespace:SocialApp" 

    and <local:BindableRichTextBox Text="Bindable Details"/>

    • Marked as answer by jaintanya1128 Thursday, December 20, 2012 9:41 AM
    Thursday, December 20, 2012 9:41 AM