SIL*_*ENT 5 c# wpf richtextbox textrange
我正在扩展WPF Richtextbox的功能。我希望某些文本在键入时变为粗体。我能够使某些文本变为粗体,但粗体字后面的文本也将变为粗体...
这是我的代码示例:
private bool _Running = false;
void CustomRichTextBox_TextChange(object sender, TextChangedEventArgs e)
{
if(_Running)
return;
_Running = true;
//Logic to see if text detected
//Logic to get TextPointers
//Logic to get TextRange
var boldMe = new TextRange(textPointer1, textPointer2);
//Bold text
boldMe.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
_Running = false;
}
Run Code Online (Sandbox Code Playgroud)
我想要:
没结没结没结没结
但是我得到的是:
没结没结没结没结
**请注意,键入时它将变为粗体。
如何防止加粗单词后的文本也加粗?
由于提供的链接的可接受解决方案适用于WinForms,其余解决方案适用于预设文本,因此没有重复的问题。
经过多次测试,我找到了一个简单的解决方案。
CaretPosition = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
Run Code Online (Sandbox Code Playgroud)
这会将插入符号设置为正确的方向,从而防止 BOLD 设置在 Run 对象中继续。
if(textPointerEnd.GetNextInsertionPosition(LogicalDirection.Forward) == null)
new Run("", textPointerEnd);
Run Code Online (Sandbox Code Playgroud)
这会将 Run 对象添加到位于 Paragraph 对象末尾的新 Bold 对象的末尾。
| 归档时间: |
|
| 查看次数: |
1960 次 |
| 最近记录: |