2016-12-01 50 views
0

我嘗試在invoke方法中更改顏色文本RickTextBox wpf。但是我遇到了一些麻煩。我的問題是'SolidBrush'參數類型對格式化屬性'Foreground'無效。參數名稱:值

「SolidBrush」參數類型不是有效的格式屬性「前景」。參數名:價值

我的代碼

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

請幫我

謝謝!

回答

2

使用WPF System.Windows.Media.Brushes類而不是System.Drawing.Brushes從的WinForms:

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

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