Posted 13 years ago
by Phil Devaney
-
Senior Software Engineer,
Serck Controls Ltd
Version: 11.1.0545
Platform: .NET 4.0
Environment: Windows 7 (32-bit)
Hi,
I've found a couple of issues related to the NewRowTemplateBehavior functionality. I have fixed them both by building my own version of the Interop assembly, but it would be good to get them rolled up into the product.
1. If you try to commit a row that has validation errors, the standard data grid behavior is to not commit and leave the row in edit mode. However, if you click on the new row template while the edit row has errors, it still adds and edits a new row. After this, the data grid gets very confused! To fix this, I added the following code in OnDataGridRowMouseLeftButtonDown:2. If you add and commit a new row, the current cell appears to be in the newly added row, but if you press F2 it doesn't edit the new row but instead creates a 2nd new row. This can be reproduced easily in the Sample Browser. To fix this, I added a new attached property called PreviousCell of type DataGridCellInfo, and then added an event handler for DataGrid.CurrentCellChanged in OnTemplatePropertyValueChanged. The handler looks like this:
This keeps track of the previously selected cell, and if the current cell tries to change to the new item row, it instead sets the current cell back to the previous cell. There may be a better way of doing this, but it seems to work OK.
Phil
I've found a couple of issues related to the NewRowTemplateBehavior functionality. I have fixed them both by building my own version of the Interop assembly, but it would be good to get them rolled up into the product.
1. If you try to commit a row that has validation errors, the standard data grid behavior is to not commit and leave the row in edit mode. However, if you click on the new row template while the edit row has errors, it still adds and edits a new row. After this, the data grid gets very confused! To fix this, I added the following code in OnDataGridRowMouseLeftButtonDown:
if ( datagrid.SelectedItem != null )
{
var selectedRow = datagrid.ItemContainerGenerator.ContainerFromItem( datagrid.SelectedItem ) as DataGridRow;
if ( selectedRow != null && selectedRow.IsEditing )
return;
}
private static void OnDataGridCurrentCellChanged( object sender, EventArgs e )
{
var datagrid = sender as DataGridControl;
if ( datagrid == null )
return;
if ( datagrid.CurrentCell.Item == CollectionView.NewItemPlaceholder )
{
var previousCell = GetPreviousCell( datagrid );
if ( previousCell.IsValid )
datagrid.CurrentCell = previousCell;
}
else
SetPreviousCell( datagrid, datagrid.CurrentCell );
}
Phil