RichTextBox复制文本时,页面加载

本文关键字:加载 复制 文本 RichTextBox | 更新日期: 2023-09-27 17:49:15

我使用以下代码来检测文本中的超链接:

将文本与链接绑定到RichTextBox

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);
        }
    }
}

我有一个列表框,现在使用这个RichTextBox,但问题是,现在每当我进入页面的文本被复制或从列表框的另一个项目的文本被添加。

RichTextBox复制文本时,页面加载

richTextBox.Blocks.Add(paragraph);之前添加richTextBox.Blocks.Clear(),以确保这里没有内容