Hello,
I'd like to check whether the user has currently the last (= most bottom right) cell in the grid selected, so I can jump back the the first cell in the grid if he tabs further.
I'm currently doing it like this. However this doesn't work if the user has altered the order of the columns and moved them around, since the last cell seems to always stay the same, even if it has been moved.
Is there maybe any "better" way to do this?
Hi,
I have been looking into your question and the issue in your scenario is that the field index is not changed when the field position is changed. In order to able to check the actual field position of the active cell you could you the following ‘if’ statement:
if (grid.ActiveCell.Record.Index == grid.Records.Count - 1 && grid.ActiveCell.Field.ActualPosition.Column == grid.FieldLayouts[0].Fields.Count - 1)
I am attaching a sample application(datagridActiveCellNavigation.zip) that shows my suggestion.
Let me know, if you need any further assistance on this matter.
Thanks, that worked well. I just had to additionally subtract the number of non-visible fields from the Fields.Count.
The only issue that I have now is that grid.ExecuteCommand(DataPresenterCommands.CellFirstOverall); doesn't go into the first cell, first row but in the second cell, first row. It works in your sample, but I'm a little stumped why it doesn't work in our application. I already removed all other events from the XamDataGrid we had, to rule out any side effects. The same happens if I use CellFirstInRecord, where it jumps to the second column in the current record. Do you maybe have an idea?
I am just checking if you require any further assistance on the matter.
Thank you for your reply. Let me know, if you need any further assistance on this matter.
Thanks, the missing e.Handled = true; seemed to be the final culprit.
I am just checking, if you need any further assistance on this matter.
Thank you for your feedback. I have been looking into your scenario and in order to skip the tabbing through the other controls in the window you need to handle the ‘KeyDown’ event when tabbing out from the last cell of the XamDataGrid like:
private void XamDataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
XamDataGrid grid = (XamDataGrid)sender;
int postion = grid.ActiveCell.Field.ActualPosition.Column;
if (e.Key == Key.Tab || e.Key == Key.Enter)
e.Handled = true;
grid.ExecuteCommand(DataPresenterCommands.CellFirstOverall);
grid.ExecuteCommand(DataPresenterCommands.StartEditMode);
}
I am attaching a modified version of your sample application(datagridfieldsettings_modified.zip) with this modification.
We found out what the issue was. Our last field in the grid used a template that modified the CellValuePresenter style. If we removed that, it worked. We first used a workaround that involved a hacky Thread.Sleep() call which made the whole stuff work:
grid.ExecuteCommand(DataPresenterCommands.CellFirstOverall);Task.Factory.StartNew(() =>
{ Thread.Sleep(50); }).ContinueWith((prevTask) => { if (grid.ActiveCell.IsActive) { CellValuePresenter.FromCell(grid.ActiveCell).Editor.StartEditMode(); } }, TaskScheduler.FromCurrentSynchronizationContext());
I've attached a slightly modified version of you sample that reproduces the issue, maybe you can tell what's going wrong there.
Thanks for your help!