基于行号的文本块中的 WPF 内联编辑

WPF Inline Editing in textblock based on linenumbers

我想突出显示文本块中的某些行。这些行是动态决定的。我该怎么做? 例如-我想突出显示所有包含文件标签的行,我该怎么做?

像下面这样的东西应该适合你:

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

如果您需要更大的灵活性,您也可以使用正则表达式来代替文本比较来进行匹配。

您可以使用内联文本块来做高亮,例如

 TextBlock tb = new TextBlock();
                    tb.TextWrapping = TextWrapping.Wrap;
                    tb.Margin = new Thickness(10);
                    tb.Inlines.Add("<xml...> ");
                    tb.Inlines.Add(new Run("<Configuration ") 
                                       { 
                                         FontWeight = FontWeights.Bold,
                                         Foreground = Brushes.Blue });

                    tb.Inlines.Add(new Run( new LineBreak())....