如何在WPF中检测用户发起的滚动事件?从用户开始滚动,我指的是由鼠标单击滚动条或用户使用滚动条的上下文菜单(如截图中的上下文菜单)引起的滚动事件。

我想知道这一点的原因是:当用户想要手动控制滚动位置时,我想要禁用我实现的自动滚动功能。通过调用ListView自动向下滚动到新添加的元素的ScrollIntoView,应该在用户进行任何手动滚动时停止这种行为。
发布于 2015-02-19 07:57:47
正如Sinatr建议的那样,我创建了一个标志,用于记住是否触发了ScrollIntoView。这个解决方案似乎工作得很好,但需要对ScrollChangedEventArgs进行一些处理。
相关的位在scrollViewer_ScrollChanged中,但是我为上下文提供了更多的代码,当用户试图向上滚动时,autoscroll就会被停用,当他滚动到底部时会重新激活。
private volatile bool isUserScroll = true;
public bool IsAutoScrollEnabled { get; set; }
// Items is the collection with the items displayed in the ListView
private void DoAutoscroll(object sender, EventArgs e)
{
if(!IsAutoScrollEnabled)
return;
var lastItem = Items.LastOrDefault();
if (lastItem != null)
{
isUserScroll = false;
logView.ScrollIntoView(lastItem);
}
}
private void scrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.VerticalChange == 0.0)
return;
if (isUserScroll)
{
if (e.VerticalChange > 0.0)
{
double scrollerOffset = e.VerticalOffset + e.ViewportHeight;
if (Math.Abs(scrollerOffset - e.ExtentHeight) < 5.0)
{
// The user has tried to move the scroll to the bottom, activate autoscroll.
IsAutoScrollEnabled = true;
}
}
else
{
// The user has moved the scroll up, deactivate autoscroll.
IsAutoScrollEnabled = false;
}
}
isUserScroll = true;
}https://stackoverflow.com/questions/28587131
复制相似问题