Welcome to the Xceed Community | Help
Community Search  
More Search Options

Keep current cell focus when resetting Items source

Sort Posts: Previous Next
  •  07-19-2012, 11:39 AM Post no. 32428

    Keep current cell focus when resetting Items source

    Hi

    We have a model for which we display a view using a WPF DataGridControl. The adapters to which Items binds are complicated to maintain, so when edits are made we propagate them to the model, throw away the adapters and create new ones from the model. It is always the case that we create as many adapters as we had before.

    Due to throwing away the old adapters the DataGridControl loses the current item/cell. So when the user made an edit, she cannot navigate using arrow keys to the next cell, as the old cell vanished and new ones were generated.

    We'd like to set focus to the new cell generated at the same row index and in the same column as the old one, but have failed to make it possible such that the focus is set and the arrow keys can be used after an edit.

    What kind of approach would you suggest to make the above scenario possible, assuming that reusing the adapters is not an option?

    Thanks for any advice.

  •  07-19-2012, 8:40 PM Post no. 32432 in reply to 32428

    Re: Keep current cell focus when resetting Items source

    Hi Ber-Sma,

    I am not sure what do you mean by "model" and "adapters"? However, have you tried to save the item which was selected before throwing the old adapters? You could try to save both, the DataGrid's CurrentItem and the SelectedItem properties, and set them back after editing. That should be enough to set the focus on the items which had it before editing. Therefore, you should be able to use the arrow keys after that, starting from the previously selected rows. Unfortunately, this mechanism could be used to retrieve the previous data item, but cannot retrieve the previous cell because of the virtualization and container recycling features of the DataGrid.


    Fawzi Bablli
    Developer Technical Support
    Xceed Software Inc.
  •  07-20-2012, 4:00 AM Post no. 32433 in reply to 32432

    Re: Keep current cell focus when resetting Items source

    Hi Fawzi

    The adapters are the binding source for the DataRow in the DataGrid. We use a DataGridCollectionViewSource.

    We tried storing DataGrid.SelectedIndex (one item will suffice, so only the index) ans DataGrid.CurrentColumn on Cell.EditEnded and then restored these values in DataGrid.ItemsSourceChangeCompleted. It did not work. The focus was on the whole DataGrid instead of a cell, so arrow keys did not work.

    I think there are two key parts to this issue. First, we replace all the adapters by new ones. I tried using Clear+Add as well as Replace only on the bound collection (ObservableCollection) in the ViewModel.

    Second, there is a probably somewhat unusual flow of actions going on, triggered by the user edit:
    * The setter of the involved property is called
    * The value is updated in the model (which is a complicated process)
    * New Adapters are created from the model (which is also complicated)
    * Those new adapters replace old ones, so there are a lot of NotifyCollectionChanged events.
    * The setter of the involved property returns.

    I think the problem is related to exchanging bound items while an EndEdit/property update is in progress.

  •  07-21-2012, 2:57 PM Post no. 32443 in reply to 32433

    Re: Keep current cell focus when resetting Items source

    Hi Ber-Sma,

    Are you setting the focus back to the DataGrid after updating its ItemsSource? I called "MyDataGrid.Focus()" method, after setting back the currentItem/Column properties of the grid, and I was able to use the arrow keys.


    Fawzi Bablli
    Developer Technical Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.