[英]WPF - How to autoscroll scrollviewer when it need to scroll?
Let me try to explain what my requirement is, first of all, here's a form with 50 fields, at the start, cursor is in the first field TextBox: 让我尝试解释一下我的要求是什么,首先,这是一个包含50个字段的表单,开始时,光标位于第一个字段TextBox中:
When I filled 10 fields, cursor now will be in Field11: 当我填写10个字段时,光标现在将位于Field11中:
Now, I want the scroll will autoscroll to a location like this when I focused in Field11 for more view: 现在,当我将重点放在Field11上以获取更多视图时,我希望滚动条可以自动滚动到这样的位置:
So if anybody understand what I'm talking about, would you please help me to solve this? 因此,如果有人了解我在说什么,请您帮我解决这个问题? Thanks! 谢谢!
You can use ScrollChangedEventArgs.ExtentHeightChange to know if a ScrollChanged is due to a change in the content or to a user action... When the content is unchanged, the ScrollBar position sets or unsets the autoscroll mode. 您可以使用ScrollChangedEventArgs.ExtentHeightChange来了解ScrollChanged是由于内容更改还是用户操作引起的...当内容不变时,ScrollBar位置将设置或取消设置自动滚动模式。 When the content havs changed you can apply autoscrolling. 内容更改后,您可以应用自动滚动。
Code behind: 后面的代码:
private Boolean AutoScroll = true;
private void ScrollViewer_ScrollChanged(Object sender, ScrollChangedEventArgs e)
{
// User scroll event : set or unset autoscroll mode
if (e.ExtentHeightChange == 0)
{ // Content unchanged : user scroll event
if (ScrollViewer.VerticalOffset == ScrollViewer.ScrollableHeight)
{ // Scroll bar is in bottom
// Set autoscroll mode
AutoScroll = true;
}
else
{ // Scroll bar isn't in bottom
// Unset autoscroll mode
AutoScroll = false;
}
}
// Content scroll event : autoscroll eventually
if (AutoScroll && e.ExtentHeightChange != 0)
{ // Content changed and autoscroll mode set
// Autoscroll
ScrollViewer.ScrollToVerticalOffset(ScrollViewer.ExtentHeight);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.