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();
}
}
}