Data Grid NewRowTemplateBehavior Issues

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 10 years ago by Phil Devaney - Senior Software Engineer, Serck Controls Ltd
Version: 11.1.0545
Platform: .NET 4.0
Environment: Windows 7 (32-bit)
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 )
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:

private static void OnDataGridCurrentCellChanged( object sender, EventArgs e )
    var datagrid = sender as DataGridControl;
    if ( datagrid == null )

    if ( datagrid.CurrentCell.Item == CollectionView.NewItemPlaceholder )
        var previousCell = GetPreviousCell( datagrid );
        if ( previousCell.IsValid )
            datagrid.CurrentCell = previousCell;
        SetPreviousCell( datagrid, datagrid.CurrentCell );
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.


Comments (1)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Phil,

For your first issue, that unfortunately only fixes the issue if the cell with the validation issue is editing and you click on the new row. If you click to another cell, then to the new row then it brings up the same issue. The two pieces of information needed to fix this are not publicly available on the DataGrid. This includes the HasRowValidationError and HasCellValidationError properties on DataGrid. If these were public, then we could check those before switching templates. Also, I don't believe this code would work if your SelectionMode is anything but FullRow.

From what I can tell, the new row behavior will just switch out the custom template with the default template (i.e. empty cells) when you click. You won't be able to edit the cells until the validation error is corrected. This is same behavior as when not using our NewRowTemplateBehavior though.

I'm not sure there is a good workaround for the other issue either. The problem is you are reseting the current cell, even when the user could be explicitly clicking in the new row to give it focus. Visually, this may work because we pass focus to the first focusable cell once we update the template, but the CurrentCell may be out of sync at that point.

Actipro Software Support

The latest build of this product (v21.1.1) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.