This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, We're trying to implement the functionality wherein upon tabbing past the last row in a datagrid, a new row is created and the first cell is set into edit mode. In a previous implementation, the Datagrid.KeyDown event was used after which a test of the selected row and column would determine if it was the last item in the grid. However, the KeyDown event does not appear to fire on the mindscape datagrid-- I'm assuming that it's getting swallowed somewhere down the visual tree? Is there way to execute a method (in our case an "add" command on the associated ViewModel) when tabbing beyond the last item in the collection? |
|
|
Hello The only possible way to do this currently would be to add another item to the end of the ItemsSource. You could create a dummy implementation of the data objects you're adding to the ItemsSource which has no values. Add this to the end of the ItemsSource and a row will be added in the DataGrid which users can edit. We do have plans to add better support for an "add" data grid row in a future enhancement. I have increased the priority of this feature. Let me know if the suggestion I made is not going to work for you. -Jason |
|
|
Creating a dummy data object is an acceptable solution, as we need to create the new object in the viewmodel anyhow. The primary issue is however that the DataGrid.KeyDown event for the TAB key is being handled somewhere within the grid, so we have no way to detect when the user attempts to go beyond the last cell (As selected cell doesn't change). Do we instead need to handle the KeyDown event for each cell? |
|
|
Hello I have not been able to find out what is swallowing the key down events. Will you be able to use PreviewKeyDown instead? -Jason |
|
|
Thanks for the tip, PreviewKeyDown is not swallowed like KeyDown is. I had assumed (incorrectly) that the behavior would be the same. The issue I'm having now is how to test for the last cell in the datagrid, as SelectedCell is always null when PreviewKeyDown occurs, so I can't test for column index. Note that SelectedItem is set properly, however. |
|
|
Hello When the data grid is using Row selection type, the SelectedCell and SelectedCells properties are not used. I have updated the code comments to clarify this. What you'll want to use is the HighlightedCell property. That is the cell that has focus which can be navigated using the Tab, Enter or arrow keys. This functions the same regardless of the data grid selection type. -Jason |
|
|
Great! Thank you for the clarification. |
|
|
Another, somewhat related question- is there a way to scroll a row into view? Or in this case, at least scroll to the bottom of the grid, where the new item has been added? |
|
|
Hello I have added a BringCellIntoView method on the DataGrid which will scroll the view port to include the given DataGridCell. Let us know how it goes. -Jason |
|
|
The BringCellIntoView() method seems to work great, thanks! However, I'm having issues with the DataGrid.GetCell() method that I need in conjunction with it. I have a behavior to select the newly added data that goes like such,
However, the GetCell() method throws an out of bounds exception when the row index is input as Items.Count - 1, however it works for values less than or equal to Items.Count - 2. Seems like the GetCell() method isn't checking against the recently updated list of items? |
|
|
Hello While looking into this issue, I've made a fix that makes it easier to achieve the behaviour you're implementing. I assume you're still listening to PreviewKeyDown, and then adding a new item to the items source if necessary. It turns out that after your PreviewKeyDown handler is called, the usual data grid tab operation is executed. This will detect that the new row has been added and will highlight the first cell of the new row and bring it into view. This means that you will no longer need any of the code in the CollectionChanged that you posted. All you will need to do is listen to the PreviewKeyDown event, and add a new item to the items source in the appropriate situation. The data grid will take care of the rest. The fix for this to work better can be downloaded through the next nightly builds. Let us know how it goes. -Jason |
|
|
Your solution works nicely for the 'Add Bottom Row' behavior we are seeking, if users were only able to add items by tabbing. Unfortunately, the user can also add new items by clicking a button which adds a new item via a command in the ViewModel, so we need to listen to the collection changed and select new items independent of how they're added. |
|
|
Good point. I have added a fix for calling the GetCell method within the collection changed handler. This will also be available through the next nightly builds. Also, within the collection changed handler, you will only need to set the HighlightedCell property. This will automatically select the appropriate row, and bring the cell into view. (this is not part of the fix but is the existing behaviour) -Jason |
|
|
Ah, good to know that the selected item will be changed by the HighlightedCell. Thank you. |
|