I'm using C# WPF , I have a DataGrid that is bound to an ObservableCollection , I want to keep focus on DataGrid's row when user is entering value with keyboard on DataGrid, I mean when last row is focus after value entered , istead of lost focus on Datagrid lets go focus on the next row
also I want to use enter instead of TAB
What I have tried:
1- First case:
if (YOUR_DATA_GRID.Items.Count > 0 && YOUR_DATA_GRID != null)
{
bool ISOK = true;
if (YOUR_DATA_GRID.Items.Count - 2 <= 2)
{
if (!(YOUR_DATA_GRID.Items[0] as CUSTOM_MODEL is null))
{
if ((YOUR_DATA_GRID.Items[0] as CUSTOM_MODEL).id is null)
{
ISOK = false;
return;
}
}
}
if (ISOK)
{
if ((YOUR_DATA_GRID.Items.Count - 2) == CURRENT_ROW_INDEX)
{
e.Row.Loaded += Row_Loaded;
}
}
}
void Row_Loaded(object sender, RoutedEventArgs e)
{
YOUR_DATA_GRID.SelectedItem = YOUR_DATA_GRID.Items[YOUR_DATA_GRID.Items.Count - 1];
YOUR_DATA_GRID.ScrollIntoView(YOUR_DATA_GRID.Items[YOUR_DATA_GRID.Items.Count - 1]);
DataGridRow dgrow = (DataGridRow)YOUR_DATA_GRID.ItemContainerGenerator.ContainerFromItem(YOUR_DATA_GRID.Items[YOUR_DATA_GRID.Items.Count - 1]);
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
2- Second case:
Using below method in these events :
RowEditEnding
AddingNewItem
interestingly this code doesn't work to focus on the next row !:
var IIDEX = CURRENT_ROW_INDEX + 1;
if (IIDEX > YOUR_DATA_GRID.Items.Count - 1)
{
IIDEX = 0;
}
DataGridRow row = (DataGridRow)YOUR_DATA_GRID.ItemContainerGenerator.ContainerFromIndex(IIDEX);
if (row is null)
{
YOUR_DATA_GRID.ScrollIntoView(YOUR_DATA_GRID.Items[IIDEX]);
row = (DataGridRow)YOUR_DATA_GRID.ItemContainerGenerator.ContainerFromIndex(IIDEX);
object item = YOUR_DATA_GRID.Items[IIDEX];
YOUR_DATA_GRID.SelectedItem = item;
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
else
{
object item = YOUR_DATA_GRID.Items[IIDEX];
YOUR_DATA_GRID.SelectedItem = item;
YOUR_DATA_GRID.ScrollIntoView(item);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
all of thing's that I've tried, didn't work
My Full Source code + Database SQL Server: https://mega.nz/file/w5ZyzbzZ#26LW3lHyO72EJnVKlzldpCczB9ePQl7gmf0xQMKEV2k
NOTE:↑ in that source code I removed my tries for Focus On next row ,
please guide me how do I do that