2012-09-29 53 views
3

我正在使用DataGrid,運行時我使某些行可見摺疊。 假設我的第4行的可視性崩潰了,我的重點是第3行,當我嘗試在第5行上使用向下箭頭鍵的幫助時,它不工作。同樣的方法,如果我專注於第五排,並想用第三排鍵向上移動,它也不起作用。 現在,我該怎麼辦?某些行在DataGrid中摺疊,我在KeyBoard導航中遇到問題

回答

3

這實際上是.Net中的一個錯誤,有一個錯誤報告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; 
       } 
      } 
     } 
    } 
}