简体   繁体   中英

'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 !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM