WPF RichTextBox 滚动到 TextPointer

发布于 2024-11-27 13:55:05 字数 174 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转角预定愛 2024-12-04 13:55:05

查看 FrameworkElement.BringIntoView 方法。我正在使用这样的东西:

public void Foo(FlowDocumentScrollViewer viewer) {
    TextPointer t = viewer.Selection.Start;
    FrameworkContentElement e = t.Parent as FrameworkContentElement;
    if (e != null)
         e.BringIntoView();
}

Have a look at the FrameworkElement.BringIntoView Method. I'm using something like this:

public void Foo(FlowDocumentScrollViewer viewer) {
    TextPointer t = viewer.Selection.Start;
    FrameworkContentElement e = t.Parent as FrameworkContentElement;
    if (e != null)
         e.BringIntoView();
}
无敌元气妹 2024-12-04 13:55:05

使用 GetCharacterRect 获取 RichTextBox 中 TextPointer 的位置:

Rect r = textPointer.GetCharacterRect(LogicalDirection.Backward);
rtb.ScrollToVerticalOffset(r.Y);

Use GetCharacterRect to get position of TextPointer in RichTextBox:

Rect r = textPointer.GetCharacterRect(LogicalDirection.Backward);
rtb.ScrollToVerticalOffset(r.Y);
清风疏影 2024-12-04 13:55:05

我有点晚了,但这里有一个更完整的答案。当前滚动偏移需要与字符位置结合起来。下面是一个将 RichTextBox 文本指针滚动到视图中心的示例:

var characterRect = textPointer.GetCharacterRect(LogicalDirection.Forward);
RichTextBox.ScrollToHorizontalOffset(RichTextBox.HorizontalOffset + characterRect.Left - RichTextBox.ActualWidth / 2d);
RichTextBox.ScrollToVerticalOffset(RichTextBox.VerticalOffset + characterRect.Top - RichTextBox.ActualHeight / 2d);

您不需要检查负数,因为滚动会导致这种情况。

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:

var characterRect = textPointer.GetCharacterRect(LogicalDirection.Forward);
RichTextBox.ScrollToHorizontalOffset(RichTextBox.HorizontalOffset + characterRect.Left - RichTextBox.ActualWidth / 2d);
RichTextBox.ScrollToVerticalOffset(RichTextBox.VerticalOffset + characterRect.Top - RichTextBox.ActualHeight / 2d);

You don't need to check for negative numbers, as the scrolling accounts for this.

一桥轻雨一伞开 2024-12-04 13:55:05

因此,如果您想知道为什么 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" the Inline 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文