在WPF中的RichTextBox中突出显示关键词

4
我正在制作一个程序,需要查找一个段落中某个关键词/关键词组出现的次数,并将这些关键词在文本中标记出来。
我已经成功创建了接口,并且可以跟踪单词出现的次数,但是我现在遇到了如何突出显示关键词所在位置的大问题。我会在下面发布我的代码,希望得到帮助,如何在richtextbox中搜索并突出显示文本。由于这是在WPF中,显然不可用richtextbox.find()。
class TextAnalyser
{
    public int FindNumberOfOccurances(List<string> keywords, string email)
    {
        int occurances = 0;
        foreach (string keyword in keywords)
        {
            occurances += email.ToUpper().Split(new string[] { keyword.ToUpper() }, StringSplitOptions.None).Count() - 1; 
        }
        return occurances;
    }

    public void TurnTextRed(List<string> keywords, string email, RichTextBox TextBox)
    {
        foreach(string keyword in keywords)
        {
        }
    }

    public List<string> ConvertTextToList(string text)
    {
        char[] splitChars = {};
        string[] ArrayText = text.Split( splitChars, StringSplitOptions.RemoveEmptyEntries);
        return ArrayText.ToList<string>();
    }

    public string GetStringFromTextBox(RichTextBox TextBox)
    {
        var textRange = new TextRange(
            TextBox.Document.ContentStart,
            TextBox.Document.ContentEnd
        );
        return textRange.Text;
    }
}

这是我的主窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AnalyseButton_Click(object sender, RoutedEventArgs e)
    {
        var textTool = new TextAnalyser();
        var keyWords = textTool.ConvertTextToList(textTool.GetStringFromTextBox(WordTextBox).Trim());
        var email = textTool.GetStringFromTextBox(EmailTextBox).Trim();
        int usesOfWord = textTool.FindNumberOfOccurances(keyWords, email);
        Occurances.Text = usesOfWord.ToString();
    }
}
2个回答

8
这里是获取richTextBox文档中所有单词的方法。
 public static IEnumerable<TextRange> GetAllWordRanges(FlowDocument document)
     {
         string pattern = @"[^\W\d](\w|[-']{1,2}(?=\w))*";
         TextPointer pointer = document.ContentStart;
         while (pointer != null)
         {
             if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
             {
                 string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
                 MatchCollection matches = Regex.Matches(textRun, pattern);
                 foreach (Match match in matches)
                 {
                     int startIndex = match.Index;
                     int length = match.Length;
                     TextPointer start = pointer.GetPositionAtOffset(startIndex);
                     TextPointer end = start.GetPositionAtOffset(length);
                     yield return new TextRange(start, end);
                 }
             }

             pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
         }
     }

你可以更改用于分割单词的模式。
最后,轻松突出你的单词。
  IEnumerable<TextRange> wordRanges = GetAllWordRanges(RichTextBox.Document);
        foreach (TextRange wordRange in wordRanges)
        {
            if (wordRange.Text == "keyword")
            {
                wordRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            }
        }

简短问题:在这里,模式(pattern)到底是做什么用的? - Needham
在编程中,将“like”用于正则表达式方法时,它的目的是什么? - Needham
"Regex.Matches" 用于从字符串中提取与模式匹配的单词。供您参考:https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.matches(v=vs.110).aspx - HungDL

6
我遇到了这个问题,但是找不到合适的解决方案。 (使用TextBox进行绑定,在运行时高亮显示多个匹配项和颜色等) 这可以根据您的需要进行扩展。这引用了一些扩展方法,它们从UIElement的adorner图层中添加/删除指定类型T的装饰程序。
 public class HighlightRule
{
    public SolidColorBrush Brush { get; set; }
    public string MatchText { get; set; }
    public HighlightRule(SolidColorBrush solidColorBrush, string matchText)
    {
        Brush = solidColorBrush;
        MatchText = matchText;
    }
    public HighlightRule(Color color, string matchText)
    {
        Brush = new SolidColorBrush(color);
        MatchText = matchText;
    }
    public HighlightRule()
    {
        MatchText = null;
        Brush = Brushes.Black;
    }
}
public class HighlightTextBox : TextBox
{
    public List<HighlightRule> HighlightRules
    {
        get { return ( List<HighlightRule>)GetValue(HighlightRulesProperty); }
        set { SetValue(HighlightRulesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for HighlightRules.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HighlightRulesProperty =
        DependencyProperty.Register("HighlightRules", typeof(List<HighlightRule>), typeof(HighlightTextBox), new FrameworkPropertyMetadata(new List<HighlightRule>(), new PropertyChangedCallback(HighlightRulesChanged)));

    private static void HighlightRulesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        HighlightTextBox tb = (HighlightTextBox)sender;
        tb.ApplyHighlights();
    }

    public HighlightTextBox() : base()
    {
        this.Loaded += HighlightTextBox_Loaded;
    }
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e);
        ApplyHighlights();
    }
    private void HighlightTextBox_Loaded(object sender, RoutedEventArgs e)
    {
        ApplyHighlights();
    }

    static HighlightTextBox()
    {
    }
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    }

    public void ApplyHighlights()
    {

        this.TryRemoveAdorner<GenericAdorner>();
        foreach(HighlightRule rule in HighlightRules)
        {
            if (!string.IsNullOrEmpty(rule.MatchText) && !string.IsNullOrEmpty(Text) &&
                Text.ToLower().Contains(rule.MatchText.ToLower()))
            {
                if (base.ActualHeight != 0 && base.ActualWidth != 0)
                {
                    int indexOf = 0;
                    do
                    {
                        indexOf = Text.IndexOf(rule.MatchText, indexOf);
                        if (indexOf == -1) break;
                        Rect rect = base.GetRectFromCharacterIndex(indexOf);
                        Rect backRect = base.GetRectFromCharacterIndex(indexOf + rule.MatchText.Length - 1, true);
                        this.TryAddAdorner<GenericAdorner>(new GenericAdorner(this, new Rectangle()
                        { Height = rect.Height, Width = backRect.X - rect.X, Fill = rule.Brush, Opacity = 0.5 }));
                        indexOf++;
                    } while (true);

                }
            }
        }

    }

}

通用装饰器/辅助方法

 public class GenericAdorner : Adorner
{
    private readonly UIElement adorner;
    private readonly Point point;
    public GenericAdorner(UIElement targetElement, UIElement adorner, Point point) : base(targetElement)
    {
        this.adorner = adorner;
        if (adorner != null)
        {
            AddVisualChild(adorner);
        }
        this.point = point;
    }
    protected override int VisualChildrenCount
    {
        get { return adorner == null ? 0 : 1; }
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        if (adorner != null)
        {
            adorner.Arrange(new Rect(point, adorner.DesiredSize));
        }
        return finalSize;
    }
    protected override Visual GetVisualChild(int index)
    {
        if (index == 0 && adorner != null)
        {
            return adorner;
        }
        return base.GetVisualChild(index);
    }
}
 public static void TryRemoveAdorner<T>(this UIElement element)
        where T:Adorner
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
        if (layer != null)
            layer.RemoveAdorners<T>(element);
    }
    public static void RemoveAdorners<T>(this AdornerLayer layer, UIElement element)
        where T : Adorner
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return;
        for (int i = adorners.Length -1; i >= 0; i--)
        {
            if (adorners[i] is T)
                layer.Remove(adorners[i]);
        }
    }
    public static void TryAddAdorner<T>(this UIElement element, Adorner adorner)
        where T: Adorner
    {
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);

        if (layer != null)
            try
            {
                layer.Add(adorner);
            }
            catch (Exception) { }
    }
    public static bool HasAdorner<T>(this AdornerLayer layer, UIElement element) where T : Adorner
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return false;
        for (int i = adorners.Length - 1; i >= 0; i--)
        {
            if (adorners[i] is T)
                return true;
        }
        return false;
    }
    public static void RemoveAdorners(this AdornerLayer layer, UIElement element)
    {
        var adorners = layer.GetAdorners(element);
        if (adorners == null) return;
        foreach (Adorner remove in adorners)
            layer.Remove(remove);
    }

The XAML

<local:HighlightTextBox FontFamily="Calibri" Foreground="Blue" FontSize="12" Text="Hello you!! And also hello to you!" TextWrapping="Wrap" Margin="5,3,0,0">
  <local:HighlightTextBox.HighlightRules>
         <local:HighlightRule Brush="Red" MatchText="you"/>
         <local:HighlightRule Brush="Blue" MatchText="also"/>
         </local:HighlightTextBox.HighlightRules>
</local:HighlightTextBox>

外观

示例高亮

1
根据代码,位于thisTryRemoveAdorner代码无法编译通过。还有TryAddAdornerGenericAdorner - ΩmegaMan
2
感谢您的更新,所有其他答案都集中在“TextBlock”上,而您的答案是独特的。 - ΩmegaMan
这个能应用到已经包含了富文本格式的richTextBox上吗?如果可以,我需要修改什么?顺便说一下:我得到了一个错误,说在GenericAdorner中缺少Point参数。 - Welcor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接