2012-09-15 67 views
1

我是WPF的RichTextBox的新手。我想知道如何使用特定顏色突出顯示文本。如何在富文本框中更改文本部分的高亮顏色?

比方說,我有一個黃色背景的富文本框,併爲其分配一個流文檔。

richTextBox.Background = Brushes.LightYellow; 
var mcFlowDoc = new FlowDocument(); 
var para = new Paragraph(); 
para.Inlines.Add(new Run("This is the first line.\n")); 
para.Inlines.Add(new Run("This is the second line.\n")); 
para.Inlines.Add(new Run("This is the third line.")); 
mcFlowDoc.Blocks.Add(para); 
richTextBox.Document = mcFlowDoc; 

我會得到什麼旁邊的第三行的高亮顏色變爲紅色呢?我不是在談論選擇高亮顏色,但正常文字highligting(如在寫字板)

如果有解決方案,我希望它在C#代碼,我想遠離XAML編輯。

+0

您已經通過[Blam](http://stackoverflow.com/a/12437503/468718)獲得了可能的解決方案(http://stackoverflow.com/users/607314/blam)可以參考[流文檔概述](http://msdn.microsoft.com/en-us/library/aa970909.aspx)上的msdn文章,它具有'FlowDocument'上的所有常用可能用法' –

回答

2
Run run = new Run("Red is the third line.\n"); 
    // run.Foreground = Brushes.Red; 
    run.Background = Brushes.Red; 
    para.Inlines.Add(run); 
+0

如果我將'Foreground ''背景'這是我想要的。謝謝! – IneedHelp