WPF DataGrid 如何实现 向下滑动以选择项目 向上滑动以反转选择

wpf datagrid how to implement swipe down to select items swipe up to invert selection

提问人:beiduoan 提问时间:11/15/2023 更新时间:11/15/2023 访问量:15

问:

wpf datagrid如何实现向下滑动选择项目向上滑动反转选择,我尝试了以下代码,但都不起作用,我知道应该用这些事件(TouchDown,TouchUp,TouchMove)来完成,我想实现类似长按选择项目和长按向上反转选择的东西。

private Point touchStartPoint;
    private Point touchEndPoint;
    private DataGridRow touchedStartRow;
    private DataGridRow touchedEndRow;
    private int nextIndex = 0;
    private int previousIndex = 0;

    private void DataGridRow_TouchDown(object sender, TouchEventArgs e)
    {
        touchStartPoint = e.GetTouchPoint(dgBookList).Position;
        var element = e.OriginalSource as FrameworkElement;
        while (element != null && !(element is DataGridRow))
        {
            element = VisualTreeHelper.GetParent(element) as FrameworkElement;
        }

        touchedStartRow = element as DataGridRow;
        previousIndex = dgBookList.Items.IndexOf(touchedStartRow.Item);
    }

    private void DataGridRow_TouchUp(object sender, TouchEventArgs e)
    {
        touchEndPoint = e.GetTouchPoint(dgBookList).Position;
        var element = e.OriginalSource as FrameworkElement;
        while (element != null && !(element is DataGridRow))
        {
            element = VisualTreeHelper.GetParent(element) as FrameworkElement;
        }

        touchedEndRow = element as DataGridRow;
        previousIndex = dgBookList.Items.IndexOf(touchedEndRow.Item);

        var direction = touchEndPoint.Y - touchStartPoint.Y;

        if (direction > 0)
        {
            // swipe down

            if (nextIndex < dgBookList.Items.Count)
            {
                var nextItem = dgBookList.Items[nextIndex] as ReservedItemInfo; 
                if (nextItem != null)
                {
                    nextItem.IsSelected = true;
                }
            }
            nextIndex++;
        }
        else if (direction < 0)
        {
            // swipe up
            if (previousIndex >= 0)
            {
                var previousItem = dgBookList.Items[previousIndex] as ReservedItemInfo; 
                if (previousItem != null)
                {
                    previousItem.IsSelected = false;
                }
            }
            previousIndex--;
        }
        nextIndex = 0;
        previousIndex = 0;
    }

    private void DataGridRow_TouchMove(object sender, TouchEventArgs e)
    {
        //var touchEndPoint = e.GetTouchPoint(dgBookList).Position;
        touchEndPoint = e.GetTouchPoint(dgBookList).Position;
        var element = e.OriginalSource as FrameworkElement;
        while (element != null && !(element is DataGridRow))
        {
            element = VisualTreeHelper.GetParent(element) as FrameworkElement;
        }

        touchedEndRow = element as DataGridRow;
        previousIndex = dgBookList.Items.IndexOf(touchedEndRow.Item);

        var direction = touchEndPoint.Y - touchStartPoint.Y;

        if (direction > 0)
        {
            // swipe down

            if (nextIndex < dgBookList.Items.Count)
            {
                var nextItem = dgBookList.Items[nextIndex] as ReservedItemInfo; 
                if (nextItem != null)
                {
                    nextItem.IsSelected = true;
                }
            }
            nextIndex++;
        }
        else if (direction < 0)
        {
            // swipe up
            if (previousIndex >= 0)
            {
                var previousItem = dgBookList.Items[previousIndex] as ReservedItemInfo; 
                if (previousItem != null)
                {
                    previousItem.IsSelected = false;
                }
            }
            previousIndex--;
        }
        nextIndex = 0;
        previousIndex = 0;
    }
C# .NET WPF 数据网格

评论


答: 暂无答案