有些行在 DataGrid 中折叠,我在键盘导航中遇到问题



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

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

一种解决方法是使用附加行为来处理向上和向下选择。下面的示例要求将 DataGrid 的 IsSyncedWithCurrentItem 设置为 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;
                }
            }
        }
    }
}

最新更新