【问题标题】:Moving Focus out of DataGrid on Enter key press on Last row in WPF MVVM?在WPF MVVM的最后一行按Enter键时将焦点移出DataGrid?
【发布时间】:2012-05-08 13:18:35
【问题描述】:

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

我正在使用 WPF MVVM

【问题讨论】:

  • 请查看我的最后一次编辑,谢谢
  • 如果您在从数据网格派生的用户控件中创建用户控件没有问题,您仍然可以在 xaml 代码中选择数据网格,您将在属性资源管理器的左侧站点中获得属性资源管理器,您可以找到事件和之后双击那个......它会在后面的代码中为你创建一个事件
  • 我没有使用代码隐藏,因为它是 mvvm。

标签: c# wpf mvvm wpfdatagrid


【解决方案1】:
 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 也可以使用此参考。将数据网格和其他一些控件放到您的窗口中。 当您到达最后一行时,它会将焦点更改为下一个控件。因为这个 if (row.GetIndex() == dataGrid1.Items.Count - 1)

,我会在你丰富最后一行时发生

我使用带有 fullrowselect 作为选择模式的数据网格。如果你想使用带有单元格选择模式的数据网格,请给我留言。

【讨论】:

  • 当我到达第一行时,它甚至会移动到下一个控件。但我在这里想要的是当我到达 null 的最后一行和第二列时,我想移动到下一个控件。
  • 我没有 CodeBehind 文件。我正在创建一个从 DataGrid 派生的 CustomDataGrid。
猜你喜欢
  • 2012-05-11
  • 2019-12-13
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多