C# 什么';与RichTextBox一起使用时,使用ApplyPropertyValue()更新
显然,当我在C# 什么';与RichTextBox一起使用时,使用ApplyPropertyValue()更新,c#,wpf,.net-4.0,C#,Wpf,.net 4.0,显然,当我在RichTextBox上使用函数ApplyPropertyValue()时,选择将停止正常工作。 我有一个RichTextBox,里面装满了一些文本 string selectText(int index, int length) { TextRange textRange = new TextRange(TB.Document.ContentStart, TB.Document.ContentEnd); if (tex
RichTextBox
上使用函数ApplyPropertyValue()
时,选择将停止正常工作。
我有一个RichTextBox,里面装满了一些文本
string selectText(int index, int length)
{
TextRange textRange = new TextRange(TB.Document.ContentStart, TB.Document.ContentEnd);
if (textRange.Text.Length >= (index + length))
{
TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward);
TB.Selection.Select(start, end);
return TB.Selection.Text;
}
return null;
}
display.Text = selectText(6, 8);
// display contains the string "arranged"
TB.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, "Red");
// display now contains the string "arrang" (should be "arranged")
display.Text = selectText(6, 8);
display
是一个文本块,用于显示用于调试的变量值,TB
是一个RichTextBox
selectText(i,length)
如果在不使用ApplyPropertyValue()的情况下使用,则该函数可以正常工作,并始终在TB
中返回正确的文本子字符串
执行上述代码后,display
包含“arrang”而不是“arrang”。另外,如果删除ApplyPropertyValue()语句,display
将按预期包含“arranged”
ApplyPropertyValue()是怎么回事?我是不是遗漏了什么