我正在我的getter工作,作为测试这个的地方,但基本上我想要完成的是当用户输入'D'的每个实例时,因为单词的第一个字母改变了颜色该文本为红色。通过将RichTextBox的Foreground绑定到Customers.TColor属性来完成颜色更改
我的TColor属性中发生了真正的逻辑问题,因为当getter只是单行(现已注释掉)时,该程序正在工作,只查找Character.Name的第一个字符:
return name.Length > 0 && name[0] == 'D' ? Brushes.Red : Brushes.Black;
但是,我正在尝试将此功能扩展到每个单词。这是我的TColor属性逻辑:
public Brush TColor
{
get
{
string[] allNames = name.Split(null);
foreach (string n in allNames)
{
if(n[0] == 'D')
{
return Brushes.Red;
}
else
{
return Brushes.Black;
}
}
return Brushes.Black;
//return name.Length > 0 && name[0] == 'D' ? Brushes.Red : Brushes.Black;
}
set
{
tColor = defaultColor;
}
}
我要做的是单独获取每个单词,因此allNames
应该将用户输入分成单个单词,然后查看每个单词中的第一个字符,如果它是D将画笔设置为红色否则将该单词的画笔设置为黑色。如果我退出我在构造Customer对象时设置的默认值,我会收到运行时错误
我想知道两件事
1.如果这是我应该尝试更改RichTextBody中单个单词的颜色的方式
2.如何有效地查看每个单词的第一个字符并改变其颜色
这是我的RichTextBody当前的样子:
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="10,0,3.4,0" Width="505">
<Label Content="Customer name:" />
<RichTextBox x:Name="richTextBox" Height="100" Width="366">
<FlowDocument>
<Paragraph Foreground="{Binding Customer.TColor}">
<Run></Run>
<Run Text="{Binding Customer.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Content="Update" Command="{Binding UpdateCommand}"/>
</StackPanel>
我的相关变量和属性如下:
private string name;
private Brush tColor;
private Brush defaultColor = Brushes.Black;
我的构造函数是:
public Customer(String customerName)
{
Name = customerName;
TColor = defaultColor;
}
我的姓名属性是:
public String Name
{
get
{
return name;
}
set
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TColor"));
//OnPropertyChanged("Name");
}
}
以防我遗漏任何内容,a repo of the project is here
答案 0 :(得分:1)
此示例使用TextChanged处理程序格式化RichTextBox中的单词。它需要适应您的实施。
<强> XAML 强>
<Grid>
<RichTextBox x:Name="RichTextBox1" />
</Grid>
<强>代码强>
public ColorizeWordWindow() {
InitializeComponent();
this.RichTextBox1.TextChanged += RichTextBox_TextChanged;
}
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) {
RichTextBox1.TextChanged -= RichTextBox_TextChanged;
int position = RichTextBox1.CaretPosition.
GetOffsetToPosition(RichTextBox1.Document.ContentEnd);
foreach (Paragraph para in RichTextBox1.Document.Blocks.ToList())
{
string text = new TextRange(para.ContentStart, para.ContentEnd).Text;
para.Inlines.Clear();
// using space as word delimiter assumes en-US for locale
// other locales (Korean, Thai, etc. ) will need adjustment
var words = text.Split(' ');
int count = 1;
foreach (string word in words)
{
if (word.StartsWith("D"))
{
var run = new Run(word);
run.Foreground = new SolidColorBrush(Colors.Red);
run.FontWeight = FontWeights.Bold;
para.Inlines.Add(run);
}
else
{
para.Inlines.Add(word);
}
if (count++ != words.Length)
{
para.Inlines.Add(" ");
}
}
}
RichTextBox1.CaretPosition = RichTextBox1.Document.ContentEnd.
GetPositionAtOffset(-position);
RichTextBox1.TextChanged += RichTextBox_TextChanged;
}
<强>结果强>