WPF RichTextBox 如何滚动到光标所在位置、滚动条操作

1.获取当前滚动条位置

//获取当前滚动条位置
richTextBox.VerticalOffset;
richTextBox.HorizontalOffset;
//获取当前光标位置
richTextBox.CaretPosition

2.滚动到开始,结束,指定位置

        //
        // 摘要:
        //     将编辑控件的视图设置为内容的末尾。
        public void ScrollToEnd();
        //
        // 摘要:
        //     将编辑控件的 " 视图到视区的开头。
        public void ScrollToHome();
        //
        // 摘要:
        //     将编辑控件的内容保存到指定的水平 偏移量。
        //
        // 参数:
        //   offset:
        //     指定滚动的水平扭曲的一个双精度值。
        public void ScrollToHorizontalOffset(double offset);
        //
        // 摘要:
        //     将编辑控件的内容保存到指定的垂直 偏移量。
        //
        // 参数:
        //   offset:
        //     指定滚动的垂直偏移量的一个双精度值。
        public void ScrollToVerticalOffset(double offset);

3.你可以通过BringIntoView方法来滚动到某个元素的位置。

DependencyObject currObj = richTextBox.CaretPosition.Parent;
FrameworkElement fe = currObj as FrameworkElement;
if (fe != null)
{
    fe.BringIntoView();
}
else
{
    FrameworkContentElement fce = currObj as FrameworkContentElement;
    if (fce != null)
    {
        fce.BringIntoView();
    }
}

 

posted @ 2016-10-03 18:13  天马3798  阅读(1943)  评论(0编辑  收藏  举报