jonny jonny - 2 days ago 5
C# Question

'SolidBrush' parameter type is not valid for formatting property 'Foreground'. Parameter name: value

I try change color text RickTextBox wpf in invoke method . But i get some trouble . My trouble is


'SolidBrush' parameter type is not valid for formatting property 'Foreground'. Parameter name: value


My code

MethodInvoker action = delegate
{
TextRange textRange = new TextRange(RtTextProcess.Document.ContentStart, RtTextProcess.Document.ContentEnd);

if (txtColor == null) txtColor = Color.Black;

int start = textRange.Text.Length;
var txt = string.Concat(DateTime.Now.ToString(), " : ", text);

if (textRange.Text == "\r\n")
{
textRange.Text = "";
}
else
{
textRange.Text += txt.ToString();
}

TextPointer start1 = textRange.Start.GetPositionAtOffset(start, LogicalDirection.Forward);
TextPointer end = textRange.Start.GetPositionAtOffset(txt.Length, LogicalDirection.Backward);
if (start1 != null && end != null)
{
RtTextProcess.Selection.Select(start1, end);
}

// My error is here
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);

string rtb = RtTextProcess.Selection.Text;
};

RtTextProcess.Dispatcher.Invoke(action);


Please help me

Thanks !

Answer

Use the WPF System.Windows.Media.Brushes class instead of System.Drawing.Brushes from WinForms:

// using System.Drawing; --- remove this
using System.Windows.Media;
...

textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
Comments