Posted 15 years ago
by Chris Chenery
Version: 9.2.0515
Platform: .NET 3.5
Environment: Windows Vista (32-bit)

Hi,
I've found two bugs with the Wpf DataGrid.Contrib's new row template. I've downloaded the latest code from codeplex, checked that the problems still exist and made my own fixes. Obviously I don't want to maintain my own version of the DataGrid.Contrib code so thought I'd let you know about the bugs and my solutions.
1. Read-only columns and the new row template don't work well together. OnDataGridRowMouseLeftButtonDown doesn't check that the cell can be edited before giving it focus (only really an issue if the very first column is read-only and you want to auto-fill values when a new row is created). Changed code:2. Setting CanUserAddRows to false and then adding rows using IEditableCollectionView.AddNew results in a crash. I just modified DataGridExtensions.cs to check for invalid indices. Changed code:
Cheers
Chris
I've found two bugs with the Wpf DataGrid.Contrib's new row template. I've downloaded the latest code from codeplex, checked that the problems still exist and made my own fixes. Obviously I don't want to maintain my own version of the DataGrid.Contrib code so thought I'd let you know about the bugs and my solutions.
1. Read-only columns and the new row template don't work well together. OnDataGridRowMouseLeftButtonDown doesn't check that the cell can be edited before giving it focus (only really an issue if the very first column is read-only and you want to auto-fill values when a new row is created). Changed code:
private static void OnDataGridRowMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
DataGridRow row = sender as DataGridRow;
if (null == row)
return;
DataGridControl datagrid = VisualTreeHelperExtended.GetAncestor(row, typeof(DataGridControl)) as DataGridControl;
if (null == datagrid)
return;
if (CollectionView.NewItemPlaceholder == row.Item) {
ControlTemplate template = GetTemplate(datagrid);
if (row.Template == template) {
row.Template = GetDefaultTemplate(datagrid);
row.UpdateLayout();
datagrid.CurrentItem = row.Item;
DataGridColumn column = datagrid.Columns.FirstOrDefault(col => !col.IsReadOnly);
if (column != null) {
DataGridCell cell = VisualTreeHelperExtended.GetAncestor(column.GetCellContent(row), typeof(DataGridCell)) as DataGridCell;
if (cell != null)
cell.Focus();
}
datagrid.BeginEdit();
}
}
}
public static DataGridRow GetRow(this DataGridControl dataGrid, object item) {
int index = dataGrid.Items.IndexOf(item);
return ((index != -1) ? dataGrid.GetRow(index) : null);
}
Chris