您的位置:首页 > 其它

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

2016-10-03 18:13 393 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: