Hi,
I'm using a XamDataGrid with the ActiveRecord bound to an element of my ViewModel. When the element of the ViewModel changes, the ActiveRecord changes accordingly and the appropriate row is activated in the grid. Now I need to prevent the user from manually changing the ActiveRecord away from the bound value.
With my current attempt, the user is unable to change the ActiveRecord by clicking on a row, but after clicking on a row, further changes to the ViewModel element are no longer reflected in the grid. If the user then clicks a second time on the grid, the grid's ActiveRecord will then snap back to the element from the ViewModel.
Is my implementation on the right track? Where am I going astray?
private void grid_RecordActivating( object sender, RecordActivatingEventArgs e){ Element currentElement = null; DataRecord currentRecord = null; if (ViewModel.getInstance().CurrentElement != null) { currentElement = ViewModel.getInstance().CurrentElement; currentRecord = grid.GetRecordFromDataItem(currentElement, true); } if (currentElement == null) { e.Cancel = true; } else if (e.Record != currentRecord) { e.Cancel = true; currentRecord.IsActive = true; } else { currentRecord.IsSelected = true; }}
private void grid_RecordActivating( object sender, RecordActivatingEventArgs e){ Element currentElement = null; DataRecord currentRecord = null; if (ViewModel.getInstance().CurrentElement != null) { currentElement =
ViewModel.getInstance().CurrentElement; currentRecord =
grid.GetRecordFromDataItem(currentElement, true); } if (currentElement == null) { e.Cancel = true; } else if (e.Record != currentRecord) { e.Cancel = true; currentRecord.IsActive = true; } else { currentRecord.IsSelected = true; }}
I believe this approach will work better for you.
Use the PreviewMouseDown event and locate the record that was clicked on. Then if the row is the active row you can allow the normal processing to take place. In this case you will enter edit mode on the cell that was clicked on.
If the record is not the active record you can set e.Handled to true and stop the normal process that would change the active row.
void xamDataGrid1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine("xamDataGrid1_PreviewMouseDown");
DependencyObject source = e.OriginalSource as DependencyObject;
if (source == null)
return;
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(source,
typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (drp == null)
if (drp.Record != null)
// A record was found, perform an action...
if (drp.Record.IsActive)
{ e.Handled = false; }
else
{ e.Handled = true; }
}
Please let me know if you have any questions.
I hadn’t heard back from you. I was wondering if you had any further questions.
If so, please feel free to contact me.