Instead typing this in many places in the code :
TextRange rangeOfText1 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);
rangeOfText1.Text = "Test";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Yellow);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
I tried to create a method that i will be able to use all along the code in many places :
private void RichTextBoxAddText(string TextContent, Brushes ForeGroundColor, FontWeights FontWeightType)
{
TextRange rangeOfText1 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);
rangeOfText1.Text = "Test";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Yellow);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
but i'm not sure if i'm using the right way with getting the Brushes and the FontWeights the second problem is that i'm getting error :
CS0721 'FontWeights': static types cannot be used as parameters
CodePudding user response:
Define an extension method:
public static class TextRangeExt
{
public static void RichTextBoxAddText(this TextRange range, string text,
SolidColorBrush foregroundColor,
FontWeight fontWeight)
{
range.Text = text;
range.ApplyPropertyValue(TextElement.ForegroundProperty, foregroundColor);
range.ApplyPropertyValue(TextElement.FontWeightProperty, fontWeight);
}
}
And now you can use the method like:
var doc = RichTextBoxLogger.Document;
new TextRange(doc.ContentEnd, doc.ContentEnd).RichTextBoxAddText("Test", Brushes.Red, FontWeights.Bold);