我第一次使用 C#,我的目標是創建一個將文本輸出到視窗的應用程式。windows 結構在 .XAML 檔案中定義,相關文本將添加到 RichTextBox。根據要附加的字串的內容,我希望文本顏色不同。IE 如果字串包含單詞“passed”,我希望文本為綠色和紅色,如果它包含“failed”。我找到了一種幾乎可行的方法,但它改變了框中所有文本的顏色,而不僅僅是所需的行。
RichTextBox 的名稱為“TextResults”,如下所示:

我使用了 ForeGround 屬性并將其分配給 SolidColorBrush 物件
if (dispText[1].Contains("passed")) textResults.Foreground = new SolidColorBrush(Colors.Green);
else if (dispText[1].Contains("failed")) textResults.Foreground = new SolidColorBrush(Colors.Red);
else textResults.Foreground = new SolidColorBrush(Colors.Black);
textScript.AppendText(dispText[0] Environment.NewLine);
textResults.AppendText(dispText[1] Environment.NewLine);
問題是,此方法將顏色應用于 RichTextBox 中的所有字串,因此假設最終字串包含“失敗”,所有文本都變為紅色。

我在網上查看了很多不同的方法,但似乎都沒有幫助,這是我的第一個堆疊溢位帖子,如果我在這篇文章中犯了任何罪過,我深表歉意。任何幫助將非常感激。
uj5u.com熱心網友回復:
public static class WpfHelperExtensions
{
public static void AppendColoredText(this RichTextBox box, string text, Color color)
{
var range = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd)
{
Text = text
};
range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
box.ScrollToEnd();
}
}
用法
if (dispText[1].Contains("passed"))
textResults.AppendColoredText(dispText[1], new SolidColorBrush(Colors.Green));
else if (dispText[1].Contains("failed"))
textResults.AppendColoredText(dispText[1], new SolidColorBrush(Colors.Red));
else
textResults.AppendColoredText(dispText[1], new SolidColorBrush(Colors.Black));
uj5u.com熱心網友回復:
用于為新添加的行著色的擴展方法:
using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace WpfApp7
{
public static class RichTextBoxExt
{
public static void ColorLine(this RichTextBox rtb, string text)
{
if (rtb.Document.Blocks.LastBlock is Paragraph par)
{
Brush brush = text.Contains("passed") ? Brushes.Green
: text.Contains("failed") ? Brushes.Red
: rtb.Foreground;
par.Inlines.Add(new Run(text Environment.NewLine) { Foreground = brush });
}
}
}
}
MainWindow.xaml.cs:
using System;
using System.Windows;
namespace WpfApp7
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Color line
rtb.ColorLine(tbox.Text);
// Clean the text box
tbox.Text = String.Empty;
}
}
}
MainWindow.xaml:
<Window x:Class="WpfApp7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<RichTextBox Grid.Row="0" x:Name="rtb" AllowDrop="True" VerticalScrollBarVisibility="Auto" Padding="2" >
<FlowDocument>
<Paragraph />
</FlowDocument>
</RichTextBox>
<Grid Grid.Row="1" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="tbox" Margin="3"></TextBox>
<Button Grid.Column="1" Click="Button_Click" Margin="3"> Add a new line </Button>
</Grid>
</Grid>
</Window>
uj5u.com熱心網友回復:
使用這個簡單的示例,與MainWindow.xaml.cs:
using System.Windows.Documents;
using System.Windows.Media;
namespace Stack2
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
AppendTextWithColor("Text1 Req passed",Brushes.Blue);
AppendText("New status eq");
AppendText("Text1 Req failed");
AppendText("Text1 Req passed");
AppendText("New status eq");
AppendText("New status failed");
}
private void AppendText(string text)
{
Paragraph par = new Paragraph(new Run(text));
if (text.Contains("passed"))
{
par.Foreground = Brushes.Blue;
} else if (text.Contains("failed"))
{
par.Foreground = Brushes.Red;
}
else
{
par.Foreground = Brushes.Black;
}
textResults.Document.Blocks.Add(par);
}
private void AppendTextWithColor(string text, Brush c)
{
Paragraph par = new Paragraph(new Run(text));
par.Foreground = c;
textResults.Document.Blocks.Add(par);
}
}
}
并且MainWindow.xaml:
<Window x:Class="Stack2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Stack2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<RichTextBox x:Name="textResults">
<FlowDocument>
<Paragraph>
This is flow content and you can <Bold>edit me!</Bold>
</Paragraph>
</FlowDocument>
</RichTextBox>
</StackPanel>
</Grid>
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521972.html
標籤:C#wpfxml富文本框
