Because System.Windows.Controls.RichTextBox
没有财产Text
要检测其值,您可以使用以下方法检测其值
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
那么,你可以改变_Text
并使用以下命令发布新字符串
_Text = _Text.Replace("pc", "Personal Computer");
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}
所以,它看起来像这样
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
_Text = _Text.Replace("pc", "Personal Computer"); // Replace pc with Personal Computer
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text; // Change the current text to _Text
}
Remark: 而不是使用Text.Replace("pc", "Personal Computer");
你可以声明一个List<String>
您可以在其中保存字符及其替换内容
Example:
List<string> _List = new List<string>();
private void richTextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
for (int count = 0; count < _List.Count; count++)
{
string[] _Split = _List[count].Split(','); //Separate each string in _List[count] based on its index
_Text = _Text.Replace(_Split[0], _Split[1]); //Replace the first index with the second index
}
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// The comma will be used to separate multiple items
_List.Add("pc,Personal Computer");
_List.Add("sc,Star Craft");
}
Thanks,
我希望你觉得这有帮助 :)