Highlight a word in a silverlight text block

I need a way to highlight text in a text block or silverlight text box. This is done to highlight search results, for example, if you try to press Ctrl + F in your browser and find a word, the browser will highlight matching words.

+3
source share
2 answers

In a text block you can use Runto highlight words, for example: -

  <TextBlock>Ordinary Text&#160;<Run Foreground="Red">Highlighted Text</Run>&#160;More Ordinary Text</TextBlock>

Xml  , , , Xaml ( XML) < , > .

+2

Silverlight. , .

WRONG

:

private void Find(RichTextBox richTextBox, string term)
{
    var builder = new StringBuilder();

    var inlines = richTextBox.Blocks
        .OfType<Paragraph>()
        .SelectMany(paragraph => paragraph.Inlines);

    foreach( var inline in inlines )
    {
        builder.Append(((Run)inline).Text);
    }

    var regex = new Regex(term);
    var matchedStrings = regex.Matches(builder.ToString());
    foreach( var item in matchedStrings )
    {
        // Whatever you want to do.
    }
}
0

Source: https://habr.com/ru/post/1792472/


All Articles