首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF ListBox OnScroll事件

WPF ListBox OnScroll事件
EN

Stack Overflow用户
提问于 2010-11-10 06:29:00
回答 1查看 9.9K关注 0票数 10

我正在试图弄清楚如何做一些(应该)相当简单的事情。

我想要的是让一个事件在ListBox控件被滚动的时候触发。ListBox是动态创建的,所以我需要一种方法来从后面的代码开始创建它(然而,XAML解决方案也很受欢迎,因为它给了我一些开始的机会)。

提前感谢您的任何想法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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);
        }
    }
}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4139341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档