如何找到 WPF RichTextBox 的可视区域?
我正在开发一个应用程序,其中 RichTextBox 中有一堆文本。 我跳到文本中的各个位置(例如跳到任意段落),这似乎可以通过将插入符号发送到该位置来工作,但我似乎无法控制插入符号在可视区域中的结束位置。
有时插入符号最终位于 RichTextBox 的顶部,有时位于底部。 如果我只对插入符号所在的行感兴趣,但我对整个段落感兴趣,那就没问题了。理想情况下,我希望每次都将插入符号放在 RichTextBox 的中间。 除非段落长于可视区域。
我的问题。 有没有办法确定 RichTextBox 的可视区域,然后计算如何正确定位插入符? 如果我有这个值,我就可以决定是将插入符号放在中间(并知道中间在哪里)还是放在顶部。 谢谢你的帮助。
I'm working on an app where I have a bunch of text in a RichTextBox. I'm jumping to various positions within the text, (hopping to an arbitrary paragraph for example) which seems to work by send the caret to that position but I can't seem to control where in the viewable area the caret ends up.
Sometimes the caret ends up at the top of the RichTextBox and sometimes at the bottom. This would be fine if I was only interested in the line that the caret is on but I'm interested in the entire paragraph.Ideally I'd like to get the caret in the middle of the RichTextBox everytime. Unless the Paragraph is longer than the viewable area.
My question. Is there a way to determine the viewable area of a RichTextBox and so do a calculation on how to position the caret properly? If I had that value I can then decided whether to put the caret in the middle (and know where the middle is) or at the top. Thanks for you help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题。
RichTextBox 有一个 VerticalOffset 属性,当添加到文本指针的位置时,将为您提供实际的偏移量。
使用偏移量,您可以使用 RichTextBox.ScrollToVerticalOffset 根据 RichTextBox.ActualHeight 属性滚动框。
一个例子:
假设我在我想要滚动到的位置有一个 TextPoint pointer1
I solved this problem.
A RichTextBox has a VerticalOffset property which when added to the position of the textpointer will give you the actual offset.
Using the offset you can use RichTextBox.ScrollToVerticalOffset to scroll the box based on the RichTextBox.ActualHeight property.
an example:
assume I have a TextPoint pointer1 at the position I want to scroll to