Cancel Selection Change

Feb 18, 2010 at 9:42 PM

I have a WPF datagrid with a list of objects that implement IEditableObject. The objects are editable in the grid. After the user edits the object and clicks to move to a different record I need to validate the object and, if necessary, cancel the row change. My objects are in an ObservableCollection which implements a currentObject. The grid binds CurrentItem to that currentObject and that works. I also have logic in the SelectionChanged event to reset the SelectedItem that works but the grid doesn't appear to know that.

The style for the selection moves to the new item even though the grid now has my currentObject as the SelectedItem AND the focus remains in the cell that failed validation. The end result is that the user sees a row that appears to be selected but really isn't and cannot edit any other cell in the grid because the cell in the original row is still open for edit.

I could live with the focus remaining in the failed cell but need the grid to show that the row with the error is still selected. Any ideas on how to make that happen? I have attempted to set e.Handled in the SelectionChanged event (along with the other logic that resets SelectedItem) as well as several other things but the closest I can get is to have both rows appear to be selected with the cell focus still in the failed cell.