2010-10-16 78 views
1

對不起,我的英語不好...... RichTextBox內容的默認值是從RichTextBox本身繼承前景色。這很好,但是如果我爲我的文本的某個部分設置了特定的Foreground顏色,那麼顯然,該部分不會繼承Foreground。如何讓我的「彩色」文本再次繼承Foreground?我試圖做類似的東西從Office Word中的「自動」色彩,但之後我設定了特定顏色的TextRange,我不知道如何來取消它:/WPF RichTextbox從TextRange中刪除前景信息

TextRange.ClearAllProperties()做什麼,我需要的,也是清除其它屬性,如FontSizeFontFamily ...

TextRange.ApplyPropertyValue(ForegroundProperty, DependencyProperty.UnsetValue)也不會做的伎倆......

回答

1

這似乎幾乎是不可能實現的,因爲沒有「RemovePropertyValue」的方法。我也嘗試了span,並得到了與你一樣的異常,所以我做了一個方法,收集TextRange中的所有段落,併爲每個分隔符創建一個跨度。我知道不太理想..無論如何,它適用於小例如,但可能很難與更復雜的東西一起工作。

private List<Span> m_spanList = new List<Span>(); 

private void c_setForegroundButton_Click(object sender, RoutedEventArgs e) 
{ 
    TextPointer textPointerStart = c_richTextBox1.Selection.Start; 
    TextPointer textPointerEnd = c_richTextBox1.Selection.End; 
    TextRange textRange = new TextRange(textPointerStart, textPointerEnd); 
    SetForeground(textRange); 
} 

private void c_clearForegroundButton_Click(object sender, RoutedEventArgs e) 
{ 
    foreach (Span span in m_spanList) 
    { 
     span.ClearValue(Span.ForegroundProperty); 
    } 
} 

public void SetForeground(TextRange textRange) 
{ 
    List<Paragraph> spannedParagraphs = new List<Paragraph>(); 
    if (textRange.Start.Paragraph != null) 
    { 
     TextRange curRange = null; 
     Block cur = textRange.Start.Paragraph; 
     do 
     { 
      spannedParagraphs.Add(cur as Paragraph); 
      // Get next range 
      curRange = new TextRange(cur.ContentStart, cur.ContentEnd); 
     } while ((textRange.End.Paragraph == null || !curRange.Contains(textRange.End.Paragraph.ContentEnd)) && (cur = cur.NextBlock) != null); 
    } 

    if (spannedParagraphs.Count == 1) 
    { 
     Span span = new Span(c_richTextBox1.Selection.Start, c_richTextBox1.Selection.End); 
     span.Foreground = Brushes.Red; 
     m_spanList.Add(span); 
    } 
    else 
    { 
     for (int i = 0; i < spannedParagraphs.Count; i++) 
     { 
      if (i == spannedParagraphs.Count - 1) 
      { 
       Paragraph paragraph = spannedParagraphs[i]; 
       // For some reason I get an exception here when I try this.. 
       //m_span = new Span(paragraph.ElementStart, c_richTextBox1.Selection.End); 
       c_richTextBox1.Selection.Select(paragraph.ElementStart, c_richTextBox1.Selection.End); 
       Span span = new Span(c_richTextBox1.Selection.Start, c_richTextBox1.Selection.End); 
       span.Foreground = Brushes.Red; 
       m_spanList.Add(span); 
      } 
      else if (i == 0) 
      { 
       Paragraph paragraph = spannedParagraphs[i]; 
       Span span = new Span(c_richTextBox1.Selection.Start, paragraph.ElementEnd); 
       span.Foreground = Brushes.Red; 
       m_spanList.Add(span); 
      } 
      else 
      { 
       Paragraph paragraph = spannedParagraphs[i]; 
       Span span = new Span(paragraph.ElementStart, paragraph.ElementEnd); 
       span.Foreground = Brushes.Red; 
       m_spanList.Add(span); 
      } 
     } 
    } 
} 
+0

感謝您的回覆,您的代碼的結果正是我所需要的,但那不是我正在尋找的。在我的情況下,用戶創建文本(在richTextbox上),因此他可以選擇不僅僅是一個特定的段落(或者只是其中的一部分)。我已經嘗試從所選文本範圍和「ClearValue(ForegroundProperty)」中創建跨度的所有內聯,但如果用戶選擇多個段落,則會出現錯誤:「'start'和'end'TextPointers不在同一段落「。 :( – Leo 2010-10-18 01:34:57

+0

Meleak,謝謝...你的代碼做了詭計,我會看看最複雜的文檔,但現在我認爲它會正常工作。 – Leo 2010-10-19 14:47:44

+0

我知道這是一個老問題,但我認爲答案是你真正在尋找的是@ jmlumpkin下面的答案。 – 2015-03-05 00:11:16

4

您還可以通過設置屬性爲null(這爲我工作清理出的背景下,例如移除高亮)

TextRange.ApplyPropertyValue(TextElement.BackgroundProperty,NULL)未設置它;