private void AppendText(RichTextBox box, string text, LogLevel level) { Color color = Color.FromRgb(0, 0, 0); switch (level) { case LogLevel.Debug: color = Color.FromRgb(200, 200, 200); break; case LogLevel.Info: color = Color.FromRgb(100, 100, 200); break; case LogLevel.Warning: color = Color.FromRgb(200, 200, 0); break; case LogLevel.Error: color = Color.FromRgb(200, 0, 0); break; } BrushConverter bc = new BrushConverter(); Paragraph paragraph = new Paragraph(); paragraph.Foreground = (Brush)bc.ConvertFromString(HexConverter(color)); paragraph.Inlines.Add(text); box.Document.Blocks.Add(paragraph); // Slower, but also works: //TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd); //tr.Text = text + "\r\n"; //try //{ // tr.ApplyPropertyValue(TextElement.ForegroundProperty, bc.ConvertFromString(HexConverter(color))); //} //catch (FormatException) { } box.ScrollToEnd(); } private void ClearText(RichTextBox box) { box.Document.Blocks.Clear(); }
270300cookie-checkC# WPF RichTextBox