C# 更改以特定字母开头的每个单词的颜色

C# 更改以特定字母开头的每个单词的颜色,c#,wpf,mvvm,C#,Wpf,Mvvm,我在我的getter中工作只是为了测试这一点,但本质上我试图完成的是,当用户为每个“D”的实例键入单词的第一个字母时,将文本的颜色更改为红色。颜色更改是通过将RichTextBox的前景绑定到my Customers.t颜色属性来完成的。 我真正的逻辑问题发生在我的TColor属性中,因为程序在getter只查找字符的第一个字符时运行(现在已注释掉)。Name: returnname.Length>0&&name[0]=“D”?画笔。红色:画笔。黑色 但是,我正在尝试将此功能扩展到每个单词。以下

我在我的getter中工作只是为了测试这一点,但本质上我试图完成的是,当用户为每个“D”的实例键入单词的第一个字母时,将文本的颜色更改为红色。颜色更改是通过将RichTextBox的前景绑定到my Customers.t颜色属性来完成的。
我真正的逻辑问题发生在我的TColor属性中,因为程序在getter只查找字符的第一个字符时运行(现在已注释掉)。Name:
returnname.Length>0&&name[0]=“D”?画笔。红色:画笔。黑色
但是,我正在尝试将此功能扩展到每个单词。以下是我的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
应该将用户输入拆分为单个单词,然后查看每个单词中的第一个字符,如果是a,则将画笔设置为红色,否则将该单词的画笔设置为黑色。如果我将在构造客户对象时设置的默认值退格,则会出现运行时错误。
我想知道两件事 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>
我的构造函数是:

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");
        }
    }

以防万一我遗漏了什么,

本例使用TextChanged处理程序格式化RichTextBox中的单词。它需要根据您的实现进行调整

XAML

<Grid>
  <RichTextBox x:Name="RichTextBox1" />
</Grid>
结果

<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;
    }