Search code examples
c#wpfxamlinlinetextblock

WPF Inline Editing in textblock based on linenumbers


I want to highlight some lines in a textblock. These lines are decided dynamically. How do I do it? For eg- I want to highlight all the lines containing files tag, how do I do it?

Content of textblock


Solution

  • Something like the following should work for you:

    foreach(System.Windows.Documents.Run run in textBlock.Inlines.OfType<System.Windows.Documents.Run>())
    {
        if (run.Text.Contains("<files ") || run.Text.Contains("</files>"))
        {
            run.Background = Brushes.Yellow;
        }
    }
    

    You can could also use regular expressions to do the matching instead of text compares if you need more flexibility.