WPF RichTextBox 滚动到 TextPointer
WPF RichtTextBox 有一种滚动方法:
RichTextBox.ScrollToVerticalOffset(double)
我想以这样的方式滚动,使某个范围或至少是它的开头进入视图。如何以有意义的方式将 TextPointer 转换为 double ?
The WPF RichtTextBox has a method to scroll:
RichTextBox.ScrollToVerticalOffset(double)
I want to scroll in such a way, that a certain range or at least the start of it comes into view. How can I convert a TextPointer to double in a meaningful way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 FrameworkElement.BringIntoView 方法。我正在使用这样的东西:
Have a look at the FrameworkElement.BringIntoView Method. I'm using something like this:
使用 GetCharacterRect 获取 RichTextBox 中 TextPointer 的位置:
Use GetCharacterRect to get position of TextPointer in RichTextBox:
我有点晚了,但这里有一个更完整的答案。当前滚动偏移需要与字符位置结合起来。下面是一个将 RichTextBox 文本指针滚动到视图中心的示例:
您不需要检查负数,因为滚动会导致这种情况。
I'm somewhat late, but here is a more complete answer. The current scroll offsets need to be combined with the character position. Here is an example that scrolls RichTextBox text pointer to the center of the view:
You don't need to check for negative numbers, as the scrolling accounts for this.
因此,如果您想知道为什么
BringIntoView()
不起作用或者它滚动到文本框的顶部,很可能是因为您试图“进入视图”Inline 包含您的整个可滚动文本内容 - 它“带来查看”此内联,该内联从(您猜对了)顶部的“开始”
TextPosition
开始。解决方案是根据Miroslav的答案使用
ScrollToVerticalOffset()
。So if you're wondering why
BringIntoView()
isn't working or it scrolls to the top of your textbox, it's likely because you are attempting to "bring into view" theInline
that comprises your entire scrollable text content - it "brings to view" this inline, which starts at (you guessed it) the "start"TextPosition
at the top.Solution is to use
ScrollToVerticalOffset()
per Miroslav's answer.