0

当我点击它的最后一行时,如何将焦点从 DataGrid 移到下一个控件Enter,这是空的。

我正在使用 WPF MVVM

4

1 回答 1

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 也可以使用此参考。将数据网格和其他一些控件放到您的窗口中。当你到达最后一行时,它会将焦点更改为下一个控件。如果(row.GetIndex()== dataGrid1.Items.Count - 1),我会在你丰富最后一行时发生

我使用带有fullrowselect 的 datagrid 作为选择模式。如果你想使用带有单元格选择模式的 datagrid,请给我留下评论。

于 2012-05-07T14:55:50.723 回答