1

全部

在将列表框滚动到顶部时,如果我仍然按住手指,列表框将被压缩一点,但当我松开水龙头时它会像往常一样弹回。有什么方法可以检测到这种行为吗?

目前我正在使用此代码来捕获滚动事件,但是当列表框到达顶部或末尾时,即使我执行上述手势,这些值也将始终为 0 或列表框的高度。

void PageLoaded(object sender, RoutedEventArgs e)
    {
        List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(listBox1);
        foreach (ScrollBar scrollBar in scrollBarList)
        {
            if (scrollBar.Orientation == System.Windows.Controls.Orientation.Horizontal)
            {
                scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(horizontalScrollBar_ValueChanged);
            }
            else
            {
                scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar_ValueChanged);
            }
        }      
    }

    private void horizontalScrollBar_ValueChanged(object sender, RoutedEventArgs e)
    { 
    }

    private void verticalScrollBar_ValueChanged(object sender, RoutedEventArgs e)
    {
        ScrollBar scrollBar = (ScrollBar)sender;
        object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty);
        object maxObj = scrollBar.GetValue(ScrollBar.MaximumProperty);
        if (valueObj != null && maxObj !=null)
        {
            double value = (double)valueObj;
            double max = (double)maxObj;

            System.Diagnostics.Debug.WriteLine(value);
            System.Diagnostics.Debug.WriteLine(max);
        }
    }

    void btnDelete_Click(object sender, EventArgs e)
    {
        ((ObservableCollection<String>)listBox1.ItemsSource).Remove("hello1");
        listBox1.ScrollIntoView(listBox1.Items[listBox1.Items.Count()-1]);
    }

    public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
    {
        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 : UIElement
    {
        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);
            }
        }
    }
4

0 回答 0