我正在寻找一种将输出到wpf富文本框中的下一行加粗的方法。因此,如果输出以00000651开头,则它将为粗体,否则将是正常的。
这里是我的意思的说明,只是这不起作用。
private void ProcessMessage(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage == 0)
{
string output = (string)e.UserState;
if (output.Substring(0, 8) == "00000651")
{
outputBox.AppendText("<Bold>"+output+"</Bold>");
}
else
{
outputBox.AppendText(output);
}
outputBox.ScrollToEnd();
}
}
如何实现?
谢谢
编辑:
我看到您已将此标记为重复,说我可以添加:
/// <summary>
/// This method highlights the assigned text with the specified color.
/// </summary>
/// <param name="textToMark">The text to be marked.</param>
/// <param name="color">The new Backgroundcolor.</param>
/// <param name="richTextBox">The RichTextBox.</param>
/// <param name="startIndex">The zero-based starting caracter position.</param>
public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox, int startIndex)
{
if (startIndex < 0 || startIndex > textToMark.Length-1) startIndex = 0;
System.Drawing.Font newFont = new Font("Verdana", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 178, false);
try
{
foreach (string line in richTextBox.Lines)
{
if (line.Contains(textToMark))
{
richTextBox.Select(startIndex, line.Length);
richTextBox.SelectionBackColor = color;
}
startIndex += line.Length +1;
}
}
catch
{ }
}
但是,此Rich文本框来自System.Windows.Controls.RichTextBox,而不是System.Windows.Forums.RichTextBox,因此此解决方案不起作用