简体   繁体   English

WPF RichTextBox:如何在Run(text)的单个句子中添加不同的颜色?

[英]WPF RichTextBox: How to add different colors in a single sentence in Run(text)?

I have a client talking to a server. 我有一个客户正在与服务器交谈。 The server may may send back the buffer as "Bob: Is anyone here?" 服务器可能将缓冲区发送回为“鲍勃:有人在吗?” You can think of it like a chatroom. 您可以将其视为聊天室。

Prior to the first occurrence of ':', I would like to make this word green. 在第一次出现“:”之前,我想将此词设为绿色。 Everything to the right of ':' will stay white. ':'右边的所有内容将保持白色。

How can I do this? 我怎样才能做到这一点? I found this not to be so straight-forward. 我发现这不是那么简单。

This writes an incoming text message to the RichTextBox: 这会将传入的文本消息写入RichTextBox:

public void WriteLine(string text)
{
    Paragraph para = new Paragraph(); 

    // Buffer output
    para.Inlines.Add(new Run(text));

    // Add block
    txtOutput.Document.Blocks.Add(para);

    // Always keep scrolled to the end
    txtOutput.ScrollToEnd();

    // Clear input field.
    txtInput.Clear();

    // Focus back on the input field.
    txtInput.Focus();
}

My attempt: 我的尝试:

// Output buffer
para.Inlines.Add(new Run { Text = text, Foreground = Brushes.Green, FontWeight = FontWeights.Bold });

The problem with doing it this way is the whole line will be green. 这样做的问题是整条线都是绿色的。 What I need is this format: 我需要的是这种格式:

Name (Green): Output (White). 名称(绿色):输出(白色)。

Please let me know. 请告诉我。 Thanks. 谢谢。

Prior to the first occurrence of ':', I would like to make this word green. 在第一次出现“:”之前,我想将此词设为绿色。 Everything to the right of ':' will stay white. ':'右边的所有内容将保持白色。

Based on your requirement in question, all you need to do is to split the text and add two Run to your Paragraph. 根据您的需求,您所需要做的就是拆分文本并将两个Run添加到您的段落中。

Check the below code. 检查以下代码。

public void WriteLine(string text)
{
    Paragraph para = new Paragraph();

    //Split the content from text

    var content = text.Split(':');

    // Buffer output
    para.Inlines.Add(new Run { Text = content[0] + ": ", Foreground = Brushes.Green, FontWeight = FontWeights.Bold });
    para.Inlines.Add(new Run { Text = content[1], Foreground = Brushes.White, FontWeight = FontWeights.Regular });

    // Add block
    txtOutput.Document.Blocks.Add(para);

    // Always keep scrolled to the end
    txtOutput.ScrollToEnd();

    //// Clear input field.
    //txtInput.Clear();

    //// Focus back on the input field.
    //txtInput.Focus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作一种方法来获取文本内容和 colors 以将带有 colors 的文本添加到 WPF 中的 RichTextBox 控件? - How to make a method that will get text content and colors to add text with colors to RichTextBox control in WPF? 如何在当前RichTextBox文本WPF中添加文本 - How to Add text in current RichTextBox text WPF 如何将多行文本作为单个段落粘贴到WPF RichTextBox? - How to paste multiline text to WPF RichTextBox as single paragraph? 如何在WPF中闪烁RichTextBox边框和背景颜色 - How to flash a RichTextBox border and background colors in WPF 如何在用户编写时在 RichTextBox 中用不同颜色为不同的单词着色,并在单击该彩色文本时引发事件 - How to color different words with different colors in a RichTextBox while a user is writing and raise an event when that colored text is clicked 在文本之间添加图像 WPF RichTextBox - Add image inbetween text WPF RichTextBox RichTextBox - 使用多种颜色添加文本到顶部(仅显示最新行) - RichTextBox - Add text to top with multiple colors (only latest line is showing) 如何从 WPF 中的 RichTextBox 获取所有文本 - How to get all text from RichTextBox in WPF 如何在Windows Phone的单个单词中以不同的颜色写文本? - How to write Text in different colors in single word in windows phone? Wpf 将文本替换为 RichTextBox - Wpf Replace Text into RichTextBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM