WPF RichTextBox-选定的块? [英] WPF RichTextBox - Selected Block?
本文介绍了WPF RichTextBox-选定的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用WPF RichTextBox,并注意到可以通过遍历RichTextBox.Document.Blocks遍历组成其文档的块.
I am experimenting with the WPF RichTextBox and notice that I can itterate through the blocks that make up its document by looping through RichTextBox.Document.Blocks.
获得包围插入符的块的最佳方法是什么?
What is the best way to get the Block that surrounds the caret?
我可以获得每个块的CaretPosition以及ElementStart和ElementEnd属性,但是看不到如何比较它们,因为除非我遗漏了一些明显的东西,否则不会公开实际的字符偏移量.
I can get the CaretPosition and the ElementStart and ElementEnd properties of each block but can't see how to compare them because the actual character offsets are not exposed unless I am missing something obvious.
推荐答案
var curCaret = richTextBox1.CaretPosition;
var curBlock = richTextBox1.Document.Blocks.Where(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
这篇关于WPF RichTextBox-选定的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文