Windows Phone ListBox轻拂行为

时间:2012-12-28 03:15:44

标签: c# silverlight xaml windows-phone

所有

在将ListBox滚动到顶部的同时,如果我仍然按下我的fingure,列表框将被压缩一点点但是当我释放水龙头时它会正常弹回。有没有办法检测到这种行为?

目前我正在使用此代码来捕获滚动事件,但是当列表框到达顶部或结尾时,值将始终为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);
            }
        }
    }

0 个答案:

没有答案