Idea for future release

Aug 29, 2008 at 2:30 PM
In the nice to have area.  I would be great if there was a property about cell advancement. (i.e. EnterKeyAdvanceBy..... Row or Column).  Right now Enter takes you to the next row and tab takes you to the next cell to the right.
Coordinator
Aug 29, 2008 at 3:30 PM
A customer asked a related question on this blog post, http://blogs.msdn.com/vinsibal/archive/2008/08/27/more-datagrid-samples-custom-sorting-drag-and-drop-of-rows-column-selection-and-single-click-editing.aspx#8904177.  Basically the customer wanted the enter key to always move to the first cell of the next row.  Customizing keyboard navigation in general for the DataGrid can be done with a similar technique that I discussed in that thread.
Aug 29, 2008 at 5:34 PM
Edited Aug 29, 2008 at 5:37 PM

 

If anyone is interested here is the code.  Mostly Vincents,  a little is mine.  This will move each cell to the right when hitting (enter or return) and will go the next row upon reaching the end of the column.

You also have to have SelectionMode = Extended or otherwise you receive an error if you try to type in the last row.  Not sure why?

Thanks Vincent

 

dataGrid1.PreviewKeyDown +=

new KeyEventHandler(dataGrid1_PreviewKeyDown);   //Place this in Window1
private

 

 

void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)

 

{

 

 

if (e.Key == Key.Enter)

 

{

 

 

int currow = dataGrid1.Items.IndexOf(dataGrid1.CurrentCell.Item);

 

 

 

int colcount = dataGrid1.Columns.Count;

 

 

 

int curcol = dataGrid1.CurrentCell.Column.DisplayIndex;

 

 

 

if (curcol < dataGrid1.Columns.Count - 1)

 

{

curcol++;

dataGrid1.CurrentCell =

 

new DataGridCellInfo(dataGrid1.CurrentItem, dataGrid1.Columns[curcol]);

 

e.Handled =

 

true;

 

}

 

 

else

 

 

{

Dispatcher.BeginInvoke(

DispatcherPriority.Input, new DispatcherOperationCallback(OnKeyDown), new object[] { e });

 

e.Handled =

 

false;

 

}

}

}

 

 

private object OnKeyDown(object arg)

 

{

 

 

object[] arguments = arg as object[];

 

 

 

KeyEventArgs e = (KeyEventArgs)arguments[0];

 

 

 

if (e.Key == Key.Enter)

 

{

 

 

int i = 0;

 

 

 

foreach (object item in dataGrid1.Items)

 

{

 

 

if (item == dataGrid1.CurrentItem)

 

 

 

break;

 

i++;

}

 

 

DataGridCell cell = GetCell(i, 0);

 

 

 

if (dataGrid1.SelectionUnit == DataGridSelectionUnit.Cell || dataGrid1.SelectionUnit == DataGridSelectionUnit.CellOrRowHeader)

 

{

 

 

// have to deselect current cell(s)

 

 

dataGrid1.SelectedCells.Clear();

}

 

// set focus to the first cell

 

 

cell.Focus();

 

// select the first cell

 

 

cell.IsSelected =

true;

 

 

 

// set the current cell to the first cell

 

 

dataGrid1.CurrentCell =

new DataGridCellInfo(dataGrid1.CurrentItem, dataGrid1.Columns[0]);

 

}

 

 

return null;

 

}