'SolidBrush'参数类型对格式化属性'Foreground'无效。参数名称:value

时间:2016-12-01 08:14:49

标签: c# wpf richtextbox textrange

我尝试在invoke方法中更改颜色文本RickTextBox wpf。但是我遇到了麻烦。我的麻烦是

  

'SolidBrush'参数类型对格式化属性'Foreground'无效。参数名称:值

我的代码

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

请帮帮我

谢谢!

1 个答案:

答案 0 :(得分:3)

使用WPF System.Windows.Media.Brushes类而不是WinForms中的System.Drawing.Brushes

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

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