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.
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 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.
Marianne,
This doesn't work either. The RecordDeactivating event gives a similar effect as when I tried the RecordActivating event. In both cases, after a mouse click anywhere on the grid, the ActiveRecord stops tracking the bound property. This is the case even with PreviewMouseDown always setting e.Handled to true. Plus, the active row can still be changed by tabbing into the grid, then using the arrow keys for navigation.
I like your product, but if I can't get this to work I'll be forced to switch to another vendor. Surely I'm not the first person to come to you with this requirement? Any other ideas?
Thanks Matt,
That worked just fine.
HI,
I am just following up on this post.
Please let me know if you need additional assistance.
Sincerely, Matt Developer Support Engineer
I am attaching a sample application, that prevents the ActiveRecord from changing if a checkbox is not checked. Once the checkbox is checked, the ActiveRecord can be changed.
I am reviewing your issue.