将带有链接的文本绑定到 RichTextBox [英] Bind text with links to RichTextBox

查看:39
本文介绍了将带有链接的文本绑定到 RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将可能包含超链接的文本绑定到 RichTextBox,以便它可以将文本显示为普通文本,将链接显示为超链接.

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

例如我有以下文字:

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

我希望文本中的链接是超链接,因此 RichTextBox 中的结果将是这样的:

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

加入我们的社交网络

http://www.facebook.com/

推荐答案

我实现了我需要的

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace NazarGrynko.UI.Controls
{
    public class MyRichTextBox : RichTextBox
    {
        private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof (string), typeof(MyRichTextBox ), 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 = (MyRichTextBox)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);
                    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 = 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);
        }
    }
}

使用示例:

<MyRichTextBox Text="{Binding Message}"/>

这篇关于将带有链接的文本绑定到 RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆