2011-09-12 76 views
1

如何突出顯示文本中單詞列表的所有匹配項。 例如,我確實有一個字符串列表(「if」,「else」,「then」,「while」,「true」)。 我確實需要在文本框中找到它們並突出顯示它們(前景+背景色)。突出顯示文本框中的特殊單詞

例子,因爲它應該是什麼樣子: enter image description here enter image description here

目前的方法是覆蓋文本框,並在OnTextChange事件做「東西」。

+0

對於更高級格式化我結束了一個的FlowDocument和FlowDocumentReader去。不同的語法,但我發現標記FlowDocument更符合邏輯。你必須解析出單詞,然後標出單詞。如果你想留在文本考慮一個RichTextBox。當您在RichTextBox中標記位置(與FlowDocument源相對)時,模型與此不同。下方是FlowDocument是複雜的。 – Paparazzi

回答

0

我實際使用使用RichTextBox的一些方法,但即時通訊製作工序慢。 實現了我如何標記的東西仍然存在一些錯誤。例如,在第一個字符後標記的所有內容都會標記爲 。因此,它看起來就像是: enter image description here

pos is the position of the character i want to mark (+1 for just one character), in OnTextChange 
MarkForeground(pos + 2, pos + 2 + 1, Colors.Green); // +2 for some awkward wpf bug probably ;) 

private void MarkForeground(int start, int end, Color col) 
    { 
     TextPointer startPointer = this.Document.ContentStart.GetPositionAtOffset(start); 
     TextPointer endPointer = this.Document.ContentStart.GetPositionAtOffset(end); 

     if (startPointer != null && endPointer != null) 
     { 

      TextRange range = new TextRange(startPointer, endPointer); 


      range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(col)); 

     } 
    } 
相關問題