Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
{
     //___________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
Posted
Comments
[no name] 10-Jan-23 13:04pm    
The observable collection tells you when a record is added. Use that event to select the same item in the DataGrid.
mojtabahakimian 10-Jan-23 15:20pm    
"The observable collection tells you when a record is added"

could you give me a sample code please ?
Graeme_Grant 10-Jan-23 17:48pm    
What Gerry is referring to is the CollectionChanged event. It fires when an item is added or removed from the collection. If you want to see how that works, refer to an answer that I posted to a different question where I use the CollectionChanged event: Cards game - data binding issue[^]. If you look at the comments at the bottom of hte answer, I posted a link to a downloadable version of the answer that you can run and see it working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900