简体   繁体   English

自动突出显示 TextBox 或 RichTextBox 中的部分文本

[英]Automatic highlighting a part of the text in a TextBox or RichTextBox

Is it possible to highlight a part of a text without selecting this part of the text preferably with a different color in Textbox or Rich TextBox?是否可以突出显示文本的一部分而不选择文本的这一部分,最好在文本框或富文本框中使用不同的颜色? In fact, I mean, a part of the text is highlighted by another color differing from the color assigned for text selection.事实上,我的意思是,文本的一部分被另一种颜色突出显示,该颜色不同于为文本选择指定的颜色。 To clarify, I have attached an image showing this behavior.为了澄清,我附上了一张显示这种行为的图片。 (The image is from a website, not WPF). (图片来自网站,而不是 WPF)。 The bold and dark green part is a text which is just highlighted, and the gray region is a selected part.粗体和深绿色部分是刚刚突出显示的文本,灰色区域是选定部分。 在此处输入图像描述

Using the RichTextBox element allows for more styling options which, to my knowledge, aren't available for the regular TextBox element.使用 RichTextBox 元素可以提供更多样式选项,据我所知,这些选项不适用于常规 TextBox 元素。

Here is an approach that I have created:这是我创建的一种方法:

// Generate example content
FlowDocument doc = new FlowDocument();

Run runStart = new Run("This is an example of ");
Run runHighlight = new Run("text highlighting in WPF");
Run runEnd = new Run(" using the RichTextBox element.");

// Apply highlight style
runHighlight.FontWeight = FontWeights.Bold;
runHighlight.Background = Brushes.LightGreen;

// Create paragraph
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(runStart);
paragraph.Inlines.Add(runHighlight);
paragraph.Inlines.Add(runEnd);

// Add the paragraph to the FlowDocument
doc.Blocks.Add(paragraph);

// Apply to RichTextBox
YourRichTextBoxHere.Document = doc;

View Screenshot查看截图

I found this article to be helpful.我发现这篇文章很有帮助。

Highlight Searched Text in WPF ListView 突出显示 WPF 列表视图中的搜索文本

While the article is about highlighting searched text in a ListView, I have easily adapted it in my own code to work with pretty much any control.虽然这篇文章是关于在 ListView 中突出显示搜索到的文本,但我已经在自己的代码中轻松地对其进行了调整,以便与几乎任何控件一起使用。

Starting with the control you pass in, it will recursively look for TextBlock s and will find the text you want, extract it as an inline, and will change it's Background / Foreground properties.从您传入的控件开始,它将递归查找TextBlock并找到您想要的文本,将其提取为内联,并更改其Background / Foreground属性。

You can easily adapt the code to be a behavior if your want.如果您愿意,您可以轻松地将代码调整为一种行为。

Here is an example:这是一个例子:

private void HighlightText(object controlToHighlight, string textToHighlight)
{
    if (controlToHighlight == null) return;

    if (controlToHighlight is TextBlock tb)
    {
        var regex = new Regex("(" + textToHighlight + ")", RegexOptions.IgnoreCase);

        if (textToHighlight.Length == 0)
        {
            var str = tb.Text;
            tb.Inlines.Clear();
            tb.Inlines.Add(str);
            return;
        }

        var substrings = regex.Split(tb.Text);
        tb.Inlines.Clear();

        foreach (var item in substrings)
        {
            if (regex.Match(item).Success)
            {
                var run = new Run(item)
                {
                    Background = (SolidColorBrush) new BrushConverter().ConvertFrom("#FFFFF45E")
                };

                tb.Inlines.Add(run);                            
            }
            else
            {
                tb.Inlines.Add(item);
            }
        }
    }
    else
    {
        if (!(controlToHighlight is DependencyObject dependencyObject)) return;

        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            HighlightText(VisualTreeHelper.GetChild(dependencyObject, i), textToHighlight);
        }
    }
}

I hope this is helpful!我希望这是有帮助的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM