private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule) { System.Windows.Controls.RichTextBox rtbx = TargetRichTextBox; var tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd); tr.Text = logMessage + "\n"; tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(GetColorFromString(rule.FontColor, (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty))) ); tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(GetColorFromString(rule.BackgroundColor, (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty))) ); tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style); tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight); if (this.MaxLines > 0) { this.lineCount++; if (this.lineCount > MaxLines) { tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd); tr.Text.Remove(0, tr.Text.IndexOf('\n')); this.lineCount--; } } if (this.AutoScroll) { rtbx.ScrollToEnd(); } }
private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule) { System.Windows.Controls.RichTextBox rtbx = TargetRichTextBox; var scrolledToEnd = AutoScroll && (TargetRichTextBox.VerticalOffset + TargetRichTextBox.ViewportHeight) >= (TargetRichTextBox.ExtentHeight - .1); var tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd); tr.Text = logMessage + "\n"; tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(GetColorFromString(rule.FontColor, (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty))) ); tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(GetColorFromString(rule.BackgroundColor, (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty))) ); tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style); tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight); if (MaxLines > 0) { while (rtbx.Document.Blocks.Count - 1 > MaxLines) { rtbx.Document.Blocks.Remove(rtbx.Document.Blocks.FirstBlock); } } if (AutoScroll && scrolledToEnd) { rtbx.ScrollToEnd(); } }