【问题标题】:Change Color of each word that starts with specific letter更改以特定字母开头的每个单词的颜色
【发布时间】:2017-03-25 05:32:27
【问题描述】:

我在 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

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案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;
        }
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 2015-07-14
      • 2020-12-12
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 2019-03-08
      • 2021-12-01
      相关资源
      最近更新 更多