Reputation: 12864
How can i remove focus from a DataGrid to next control when i hit Enter on the last row of it, which is null.
I am using WPF MVVM
Upvotes: 0
Views: 2584
Reputation: 710
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();
}
}
}
set this event as key up of your datagrid . use this refrence too using System.Windows.Controls.Primitives . put datagrid and some other control to your window . when you get to last row it will change focuse to next control. i will occur when you rich last row because of this if (row.GetIndex() == dataGrid1.Items.Count - 1)
i use datagrid with fullrowselect as selection mode.if you want use datagrid with cell selection mode leave comment for me please.
Upvotes: 2