2016-12-01 3 views
0

J'essaie de changer le texte de couleur RickTextBox wpf dans la méthode invoke. Mais j'ai des problèmes. Mon problème estLe type de paramètre 'SolidBrush' n'est pas valide pour la mise en forme de la propriété 'Foreground'. Nom du paramètre: valeur

Le type de paramètre 'SolidBrush' n'est pas valide pour la mise en forme de la propriété 'Foreground'. Nom du paramètre: Valeur

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

S'il vous plaît aidez-moi

Merci!

Répondre

2

Utilisez la classe WPF System.Windows.Media.Brushes au lieu de System.Drawing.Brushes de WinForms:

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

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