2012-04-24 33 views
0

當我點擊在最後一行輸入時,如何從DataGrid中移除焦點到下一個控件,它是空的。將焦點移出DataGrid回車鍵按下WPF MVVM中的最後一行?

我使用WPF MVVM

+0

看到我最後的編輯,請感謝 – 2012-05-08 05:11:49

+0

如果要創建用戶控制用戶控制沒有問題,從數據網格導出您仍然可以選擇在XAML代碼,你會得到屬性的探險家在財產資源管理器,你可以找到左側的網站上的數據網格事件和之後,雙擊...它會在代碼後面創建一個事件 – 2012-05-08 12:29:14

+0

我不使用代碼隱藏,因爲它是mvvm。 – 2012-05-08 14:43:40

回答

2
private void dataGrid1_KeyUp(object sender, KeyEventArgs e) 
     { 
      DependencyObject dep = (DependencyObject)e.OriginalSource; 
      //here we find the Row is selected 
      //then we check is the row last row 

      while ((dep != null) && !(dep is DataGridRow) && !(dep is DataGridColumnHeader)) 
      { 
       dep = VisualTreeHelper.GetParent(dep); 
      } 

      if (dep == null) 
       return; 

      if (dep is DataGridRow) 
      { 
       DataGridRow row= dep as DataGridRow; 
       //untill here we find the selected row and here we check if it 
       //the last row our focus go to next control after datagrid 
       if (row.GetIndex() == dataGrid1.Items.Count - 1) 
      { 
       dataGrid1.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down)); 
       dataGrid1.UnselectAll(); 
      } 
      } 
     } 

設定此事件的關鍵你的DataGrid的。使用System.Windows.Controls.Primitives也使用這個參考。把datagrid和其他一些控件放到你的窗口中。當你到達最後一行時,它將改變集中到下一個控制。我會出現在你富有最後一排,因爲這如果(row.GetIndex()== dataGrid1.Items.Count - 1)

我使用數據網格與fullrowselect作爲選擇模式。如果你想使用數據網格細胞選擇模式請給我留言。

+0

當我到達第一排時,它甚至移動到下一個控件。但我想在這裏是當我到達最後一行和第二列在null然後我想要移動到下一個控制。 – 2012-05-07 20:53:04

+0

我沒有CodeBehind文件..我創建一個從DataGrid派生的CustomDataGrid。 – 2012-05-08 10:21:23

相關問題