0

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 focused after value entered , instead of lost focus on Datagrid let's go to focus on the next row

also I want to use enter instead of TAB

but these solutions in stack overflow doesn't work for me ! : WPF DataGrid - How to stay focused on the bottom of the DataGrid as new rows are added?

.

What have I tried :

1- First case:

private void DGR_SUB_INVOLST_LoadingRow(object sender, DataGridRowEventArgs e)
{
            
  
  if (YOUR_DATA_GRID.Items.Count > 0 && YOUR_DATA_GRID != null)
    {
         //___________This piece of code is to go to the next line with enter for a single line that is entered for the first time and not to focus on the new line.
             bool ISOK = true;
             //If there is no more than one line
             // The reason for minus 2 is that because we gave something to it in Add New Item, a new line is added, so if we see 1 line
             //It means that the number of items is 2, item zero, the first line, item 1, a new line named New Place Holder
            if (YOUR_DATA_GRID.Items.Count - 2 <= 2)
            {
               //Don't get null error from where you delete the line and then refresh
                if (!(YOUR_DATA_GRID.Items[0] as CUSTOM_MODEL is null))
                {
                   // if is new
                    if ((YOUR_DATA_GRID.Items[0] as CUSTOM_MODEL).id is null)
                    {
                        ISOK = false;
                        return;
                    }
                }
            }
           //If the above condition is not met and there is also the last line, let's put the focus on the new line
            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

Mojtabah
  • 46
  • 9

0 Answers0