2011-04-01 24 views

Respuesta

29

Intenta esto:

TextRange tr = new TextRange(rtb.Document.ContentEnd,­ rtb.Document.ContentEnd); 
tr.Text = "textToColorize"; 
tr.ApplyPropertyValue(TextElement.­ForegroundProperty, Brushes.Red); 
+0

hermoso gracias! – Aks

9

Si lo desea, también puede hacer que sea un método de extensión.

public static void AppendText(this RichTextBox box, string text, string color) 
{ 
    BrushConverter bc = new BrushConverter(); 
    TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd); 
    tr.Text = text; 
    try 
    { 
     tr.ApplyPropertyValue(TextElement.ForegroundProperty, 
      bc.ConvertFromString(color)); 
    } 
    catch (FormatException) { } 
} 

Esto hará que sea lo que sólo puede hacer

myRichTextBox.AppendText("My text", "CornflowerBlue"); 

o en hexadecimal como

myRichTextBox.AppendText("My text", "0xffffff"); 

Si la secuencia del color que escribe no es válido, sino que simplemente lo escribe en el color predeterminado (negro). Espero que esto ayude!

Cuestiones relacionadas