0
votes

I am using WPF microsoft technology in my project. I have a datagrid that binds with a viewmodel property of datatype ICollectionViewLiveShaping. I want to commit datagrid row when tab is pressed and the row at the bottom for adding items will be automatically focused. This is automatically achieved when I pressed enter key. I have to do the same when tab is pressed.

I will be very thankful, if someone helps me on this.

1
Do you have some code to show what you have done so far?FredM
Does it matter where the focus is at the time of pressing the tab key? I assume you'd still want to be able to move the focus from cell to cell? Or do you want to save the changes on every focus change?Emond
You can use the "CellEditEnding" event to save your changes, it gets called when you tab from one cell to the otherRaviraj Palvankar
How about you simply 'fake' an enter click when tab is pressed by implementing an own KeyDown event which calls the default enter-KeyDownEvent, when the Key was tab? private void DataGrid_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Tab) { DataGrid_KeyDown(sender, new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, Key.Return)); } }Azzarrel

1 Answers

0
votes

If you wanna do it in MVVM pattern. then bound Keyboard keyhandler to viewmodel. and in case of TAB key use below code.

List<string> yourCollection = new List<string>(); // your view model collection
ICollectionView yourCollectionView = new CollectionViewSource { Source = yourCollection }.View;
ListCollectionView listCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(yourCollectionView);
if (listCollectionView != null)
{
    if (listCollectionView.IsAddingNew)
    {
        listCollectionView.CommitNew();
    }
    if (listCollectionView.IsEditingItem)
    {
        listCollectionView.CommitEdit();
    }
}

if you want to do it in view itself then use below code.

this.ItemsGrid.CommitEdit(); 
this.ItemsGrid.CancelEdit();

Please vote in case this help you.