我正在试图弄清楚如何做一些(应该)相当简单的事情。
我想要的是让一个事件在ListBox控件被滚动的时候触发。ListBox是动态创建的,所以我需要一种方法来从后面的代码开始创建它(然而,XAML解决方案也很受欢迎,因为它给了我一些开始的机会)。
提前感谢您的任何想法。
发布于 2010-11-10 06:53:34
在XAML中,您可以访问ScrollViewer并添加如下事件:
<ListBox Name="listBox" ScrollViewer.ScrollChanged="listBox_ScrollChanged"/>
更新
这可能是你在后台代码中所需要的:
List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(listBox);
foreach (ScrollBar scrollBar in scrollBarList)
{
if (scrollBar.Orientation == Orientation.Horizontal)
{
scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(listBox_HorizontalScrollBar_ValueChanged);
}
else
{
scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(listBox_VerticalScrollBar_ValueChanged);
}
}
使用GetVisualChildCollection的实现:
public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
List<T> visualCollection = new List<T>();
GetVisualChildCollection(parent as DependencyObject, visualCollection);
return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
{
visualCollection.Add(child as T);
}
else if (child != null)
{
GetVisualChildCollection(child, visualCollection);
}
}
}
https://stackoverflow.com/questions/4139341
复制相似问题