Добрый день всем. Я переписываю академическое приложение, написанное на Windows Forms. Хочу перенести в WPF. Пока не удалось разобраться с RichTextBox и его свойствами.
//chatBox - это имя RichTextBox;
У меня есть метод, который выводит сообщения в RichTextBox Код используемый в Windows Forms
private void AddMessage(string Content,string Color = "Black")
{
if(InvokeRequired)
{
Invoke(_addMessage,Content,Color);
return;
}
chatBox.SelectionStart = chatBox.TextLength;
chatBox.SelectionLength = Content.Length;
chatBox.SelectionColor = getColor(Color);
chatBox.AppendText(Content + Environment.NewLine);
}
private Color getColor(string text)
{
if (Color.Red.Name.Contains(text))
return Color.Red;
return Color.Black;
}
Я попытался взять эти же свойства в WPF, но не вышло. Как можно переделать код?
Код WPF
`private void AddMe`ssage(string Content, string Color = "Black")
{
if (Dispatcher.CheckAccess()) // InvokeRequired
{
Dispatcher.Invoke(_addMessage, Content, Color);
//Dispatcher.Invoke(_addMessage, Content);
return;
}
//chatBox.SelectionStart = chatBox.TextLength;
//chatBox.SelectionLength = Content.Length;
//chatBox.SelectionColor = getColor(Color);
chatBox.AppendText(Content + Environment.NewLine);
}
private Color getColor(string text)
{
if (Color.Red.Name.Contains(text))
return Color.Red;
return Color.Black;
}