本文主要是介绍WPF中判断滚动条滚动条滑倒了最底端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先是获取滚动条的方法
(可以获取到空间内部自带的ScrollViewer)
#region 获取所有控件子级元素的方法,返回该类型的List集合
public static List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T)
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child));
}
return childList;
}
#endregion
#region 内部滚动条滚动到底部后接着滚动外部滚动条
private void 控件名_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
List<ScrollViewer> scrollList = GetChildObjects<ScrollViewer>((控件的类型)sender);//查找改控件内部的ScrollViewer集合
ScrollViewer scroll = scrollList[0];
double delta = e.Delta;
double verticalOffset = scroll.VerticalOffset;
if (delta < 0)
{
scroll.ScrollToVerticalOffset(verticalOffset += 25);
if (IsVerticalScrollBarAtButtom(scroll))
{
List<ScrollViewer> parentscrollList = GetChildObjects<ScrollViewer>((控件的类型)上一节控件的父级控件);
ScrollViewer parentScroll = parentscrollList[0];
double parentdelta = e.Delta;
double parentverticalOffset = parentScroll.VerticalOffset;
if (listboxdelta < 0)
{
parentScroll.ScrollToVerticalOffset(parentverticalOffset += 25);
}
else
{
parentScroll.ScrollToVerticalOffset(parentverticalOffset -= 25);
}
}
}
else
{
scroll.ScrollToVerticalOffset(verticalOffset -= 25);
}
}
#endregion
#region 判断滚动条是否到达最底部的代码 需要传进来一个ScrollViewer对象
public bool IsVerticalScrollBarAtButtom(ScrollViewer s)
{
bool isAtButtom = false;
double dVer = s.VerticalOffset;
double dViewport = s.ViewportHeight;
double dExtent = s.ExtentHeight;
if (dVer != 0)
{
if (dVer + dViewport == dExtent)
{
isAtButtom = true;
}
else
{
isAtButtom = false;
}
}
else
{
isAtButtom = false;
}
if (s.VerticalScrollBarVisibility == ScrollBarVisibility.Disabled
|| s.VerticalScrollBarVisibility == ScrollBarVisibility.Hidden)
{
isAtButtom = true;
}
return isAtButtom;
}
#endregion
这篇关于WPF中判断滚动条滚动条滑倒了最底端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!