在DataGrid中折叠了一些行,我在KeyBoard导航中遇到问题

时间:2012-09-29 05:57:30

标签: wpfdatagrid

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

1 个答案:

答案 0 :(得分:3)

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

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