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 the solution for you would be to use the RecordDeactivating event and compare the e.Record to your data source’s CurrentActive property. If they are not the same then you could set e.Cancel to true.
Hi. Sorry about the late reply, hadn't yet had a chance to try out your code.
This approach does prevent a row from being activated when directly clicked, but the user is still able to change the active record by performing a mouse down, hold, and drag to any other row. The active record can also still be changed by clicking on the active record and using the keyboard up and down arrows. In my application it is important that the user is entirely unable to change the active record away from its binding.
I was hoping that I could cut off any such user actions through the RecordActivating event, because using OnMouseDown, onKeyDown, onMouseDrag, ect... would require me to explicitly know all the input events that could possibly lead to a change in the ActiveRecord. Likely an error prone approach.
I'm kind of at a dead end on this one and really appresiate your help (despite my late reply). Any other ideas?
I hadn’t heard back from you. I was wondering if you had any further questions.
If so, please feel free to contact me.
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.