4

我正在使用 DataGrid,运行时我可以看到折叠一些行。假设我的第 4 行的可见性是折叠的,并且我的重点是第 3 行,当我尝试在向下箭头键的帮助下移动到第 5 行时,它不起作用。同样,如果我专注于第 5 行并想使用向上箭头键移动到第 3 行,它也不起作用。现在,我该怎么办?

4

1 回答 1

4

这实际上是 .Net 中的一个错误,这里有一个错误报告

一种解决方法是使用附加行为来处理向上和向下选择。以下示例要求将 DataGrid 的 IsSynchronizedWithCurrentItem 设置为 true。

笔记!确保将 while 条件更改为适当的方式来确定项目是否折叠。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace DataGridGroupingTest
{
    class DataGridKeyboardNavigationAttachedBehavior
    {
        public static readonly DependencyProperty
                KeyboardKey
                    = DependencyProperty.RegisterAttached(
                        "IsKeyboardNavigationEnabled",
                        typeof(bool),
                        typeof(DataGridKeyboardNavigationAttachedBehavior),
                        new PropertyMetadata(
                            false,
                            OnIsKeyboardNavigationEnabledChanged));

        public static bool GetIsKeyboardNavigationEnabled(DependencyObject depObj)
        {
            return (bool)depObj.GetValue(KeyboardKey);
        }

        public static void SetIsKeyboardNavigationEnabled(DependencyObject depObj, bool value)
        {
            depObj.SetValue(KeyboardKey, value);
        }

        private static void OnIsKeyboardNavigationEnabledChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            DataGrid dataGrid = depObj as DataGrid;
            if (dataGrid != null)
            {
                dataGrid.PreviewKeyDown += dataGrid_PreviewKeyDown;
                dataGrid.IsSynchronizedWithCurrentItem = true;
            }
        }

        static void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid != null && dataGrid.CurrentCell != null)
            {
                if (e.Key == System.Windows.Input.Key.Down || e.Key == System.Windows.Input.Key.Up)
                {
                    ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid.Items);

                    int loopCount = 0;
                    do
                    {
                        if (e.Key == System.Windows.Input.Key.Down)
                        {
                            view.MoveCurrentToNext();
                            if (view.IsCurrentAfterLast)
                            {
                                view.MoveCurrentToFirst();
                                loopCount++;
                            }
                        }
                        if (e.Key == System.Windows.Input.Key.Up)
                        {
                            view.MoveCurrentToPrevious();
                            if (view.IsCurrentBeforeFirst)
                            {
                                view.MoveCurrentToLast();
                                loopCount++;
                            }
                        }
                    } while (((Person)view.CurrentItem).Boss != null && !((Person)view.CurrentItem).Boss.IsExpanded && loopCount < 2);

                    // We have to move the cell selection aswell.
                    dataGrid.CurrentCell = new DataGridCellInfo(view.CurrentItem, dataGrid.CurrentCell.Column);

                    e.Handled = true;
                    return;
                }
            }
        }
    }
}
于 2013-03-25T13:54:34.107 回答