【发布时间】:2018-05-09 03:31:31
【问题描述】:
我有一个富文本框,允许用户突出显示文本。正在加载的文本来自一个简单的纯文本文件。但我需要存储突出显示文本的绝对开始和结束字符位置(相对于文档开头),以便在保存时可以重新加载突出显示。
到目前为止,我可以这样做来应用突出显示
private void textBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
HighlightWordInTextBox(this.textBox, this.textBox.Selection.ToString(), new SolidColorBrush(Colors.Yellow));
}
public void HighlightWordInTextBox(RichTextBox textbox, string word, SolidColorBrush color)
{
TextRange tr = new TextRange(this.textBox.Selection.Start, this.textBox.Selection.End);
tr.ApplyPropertyValue(TextElement.BackgroundProperty, color);
}
但我在选择的开始或结束对象内的任何地方都看不到任何提供字符位置的东西?几乎所有方法都返回另一个 TextPointer - 但是如何从 TextPointer 中获取字符位置?
假设所有文本都加载到单个
this.textBox.Document.Blocks.Add(new Paragraph(new Run(fullText)));
编辑:
在调试时的即时窗口中,我可以访问名为 CharOffset 和 Offset 的内容,但不能在源代码中这样做,它会给出编译错误。这些属性虽然在运行时检查对象时存在,但它们不在文档中。
然而……
【问题讨论】: