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

标签 wpfdatagrid

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

最佳答案

这其实是.Net的一个bug,有bug报告here .

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

关于wpfdatagrid - 某些行在 DataGrid 中折叠,我在键盘导航中遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650343/

相关文章:

wpf - 包装在面板MVVM中的用户控件

wpf - WPF DataGrid 单元格的条件格式取决于数据类型

c# - 升级到 .NET 4.5 : An ItemsControl is inconsistent with its items source

wpf - 在 Wpf 的 DataGrid 中更改单元格的 FlowDirection

mvvm - View 中的 MouseDoubleClick 事件

wpf - 如何在 WPF 中对 DataGrid 进行 LeanFt 测试?

c# - 向WPF Datagrid添加很多项目会消耗大量CPU时间

c# - 按 T​​ab 键时选择在数据网格中丢失

WPF DataGrid垂直线粗细

c# - 使用 WPF 数据网格时如何更改列标题背景颜色