是否可以将TextPointer转换为Integer

本文关键字:转换 Integer TextPointer 是否 | 更新日期: 2023-09-27 17:59:03

我一直在编辑一些长期存在的代码,以实现一些新功能。这需要用richtextbox替换textbox,同时保持现有实现不变。在我找到插入索引之前,富文本框的属性与文本框相当。原始行(适用于文本框)是:

this.ViewModel.Body = this.ViewModel.Body.Insert(this.txtEditor.CaretIndex, link); 

所以当我把"txtEditor"改为richtextbox时,我实现了这个:

this.ViewModel.Body = this.ViewModel.Body.Insert(this.txtEditor.CaretPosition, link);

我相信它们是相似的,但是"Caret Position"的类型是TextPointer,而"CaretIndex"是一个整数,并且Body.Insert方法只将整数作为其第一个参数。

有没有什么方法可以将TextPointer转换为Integer?如果有,如何转换?

感谢

是否可以将TextPointer转换为Integer

这有点复杂,但可以使用一些技巧获得插入符号索引:

var start = txtEditor.Document.ContentStart;
var here = txtEditor.CaretPosition;
var range = new TextRange(start, here);
int indexInText = range.Text.Length;

使用:

this.ViewModel.Body = this.ViewModel.Body.Insert(indexInText, link);