Handling Enter like Tab and New Row issues

5 posts, 1 answers
  1. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 16 Apr 2020 Link to this post

    Sorry for posting 2 different issues in one thread, but they are kinda intertwined in my situation here.

    The first thing I'm wanting to do is make it so the Enter key within the RadGridView is treated like the Tab key instead. I was able to successfully do this via a custom KeyboardCommandProvider, by making it call DefaultKeyboardCommandProvider's ProvideCommandsForKey with Key.Tab is the key was Key.Enter.

    The second thing I'm wanting to do is specially handle what happens with new rows. There are 3 columns in the grid and the first 2 are being validated via INotifyDataErrorInfo while the last has no validation. I'm wanting to do the following with a new row:

    • On starting the new row, the first column is always selected. (I was able to do this without issues inside the AddingNewDataItem event, so this isn't a problem.)
    • On attempting to leave the first cell, I want to check the value and prevent progression if that value exists elsewhere in the same column. (I was able to do this without issues inside the CellValidating event, so this isn't a problem either.)
    • When pressing Enter or Tab in the last column, I want to get out of edit mode, select the new row after it has been added to the grid, and then scroll down to that new row. (This is where I am having all my issues, as I'll explain further below.)

    What I've tried doing is inside of my custom KeyboardCommandProvider, if the key is Key.Tab or Key.Enter, Shift isn't being held down, and if I am in the last column (checking the grid's current column DisplayIndex and checking if it is 2) and in a new row (which I check by seeing if my view model's current selection is null, which I set inside the AddingNewDataItem event), then I was trying to remove the default actions of select current unit and begin edit (so the only action left is move next) and add in a custom command to attempt to select the new row (which I've stored inside of the RowEditEnded event).

    My custom command for attempting the new row selection consists of canceling the grid view's edit, setting the CurrentCellInfo of the grid to the new item in the first column, and then using ScrollIntoViewAsync for the same item and column.

    The issues I'm running in to:

    • I originally was not removing Tab's default commands or calling my custom command, and instead was trying to do the above stuff from the custom command within the SelectionChanged event. As I was storing the row inside the RowEditEnded event, I was also trying to change the current selected item in my view model through that event, which meant that running the commands in SelectionChanged only partially worked because the rest of the grid view's code for the RowEditEnded event after my callback would change the CurrentCellInfo.
    • I then tried the method I explained above with changing the commands for the Tab key, and if I press Tab instead of Enter, I've been finding that sometimes the grid view will retain focus and run my custom command and other times it'll move focus to the next control in the window and never run my custom command as a result.
    • In the cases when the above does retain focus on the grid view, while I think I solved the issues with CurrentCellInfo, scrolling the grid view to the new row has been hit or miss. Sometimes it works and the success callback is called, sometimes it doesn't work and the failure callback is called instead. I've also noticed that while the current cell has the proper selection border on it, I've also seen a different cell in the grid have the focus border on it.
    • The last thing I tried was removing all the commands entirely if the Tab key is pressed (but leave things as above for the Enter key, that is only the move next and my custom command) in that same situation, but because of that, I've found that the grid view loses focus.

    If need be, I can provide a minimal example of what I've currently got that isn't working.

  2. Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    720 posts

    Posted 21 Apr 2020 Link to this post

    Hello,

    Thank you for the provided information. 

    May I ask you to check out the following article in our documentation: Scroll to Newly Added Item? In order to achieve the third requirement, you can use the demonstrated in the article approach and also set SelectedItem of the RadGridView.  

    I have prepared a sample project, where I was able to successfully select the newly added row and scroll to it. Please, check it out and let me know, if you find it helpful.

    Regards,
    Vladimir Stoyanov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 21 Apr 2020 Link to this post

    So I attempted to utilize the Items.CollectionChanged event of the RadGridView as described in the above post. This does seem to make scrolling to the new row work properly.

    I'm still having issues with selecting the new row, though. I have put up a test project at https://www.cyberbotx.com/TelerikRadGridViewProblems.zip (I was unable to attach it to this reply). This project isn't exactly as what I described in the first post as I've modified things to be very minimal and to take the above post in to account.

    What is happening in my test project is that even though the row is selected and scrolled to, the current cell isn't set properly, possibly because of timing of setting the current cell in the Items.CollectionChanged event.

    As a note, the test project uses PostSharp for various things (such as INotifyPropertyChanged and XAML Commands). This is how my actual project is as well, and the proper packages are coming from NuGet.

  4. Answer
    Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    720 posts

    Posted 24 Apr 2020 Link to this post

    Hello,

    Thank you for the shared project. 

    I have updated it to demonstrate a possible approach for achieving the described behavior. It uses the callback of the ScrollIntoViewAsync method in order to set the current cell. I also changed the logic in the keyboard command provider to only commit the edit, since the movement to the added item is handled in the CollectionChanged event. Do check it out and let me know, if it helps.

    On a side note, I had to remove the PostSharp reference, since I do not have a valid license. Additionally, I used the EventToCommandBehavior to quickly test the scenario without the missing reference. 

    Regards,
    Vladimir Stoyanov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  5. CyberBotX
    CyberBotX avatar
    11 posts
    Member since:
    Nov 2016

    Posted 18 May 2020 in reply to Vladimir Stoyanov Link to this post

    Sorry for the delay in getting back to this, but that example does work for me, even in my actual project. I've marked it as the answer.
Back to Top