WPF Datagrid validation throw "Edit level mismatch in UndoChanges" exception when cancel

WPF Datagrid validation throw "Edit level mismatch in UndoChanges" exception when cancel

Old forum URL: forums.lhotka.net/forums/t/7033.aspx


steven99 posted on Monday, June 01, 2009

I found a problem when binding with Datagrid(WPF ToolKit).
If I make a validation error in one of the cell. It will throw "Edit level mismatch in UndoChanges" exception when I click Cancel.

It can be reproduced in ProjectTracker sample:
Add reference to WPFToolKit to PTWpf project
Add this namespace to beginning of RolesEdit.xaml
xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Then add the following code to RolesEdit.xaml
<toolkit:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}"/>
  <toolkit:DataGrid.Columns>
    <toolkit:DataGridTextColumn Binding="{Binding Id,ValidatesOnDataErrors=True}" />
    <toolkit:DataGridTextColumn Binding="{Binding Name,ValidatesOnDataErrors=True}" />
  </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

Run the project and "Log in", and then "Edit roles"
First edit the role Name so that Cancel button is enabled
Then edit the role Id to same value as another row.
Click Cancel button, an exception will be thrown in this line:
if (this.EditLevel - 1 != parentEditLevel)
  throw new UndoException(string.Format(Resources.EditLevelMismatchException, "UndoChanges"));
where this.EditLevel=2 but parentEditLevel=0

I think it is the problem of Datagrid. But maybe a workaround in CslaDataProvider or UndoableBase may fix that.
Does anyone have any workaround?

RockfordLhotka replied on Tuesday, June 02, 2009

It sounds like a problem in the datagrid control - they should be calling IEditableObject.CancelEdit() (or EndEdit()) when focus leaves the row, thus bringing the child object's edit level to the same as the collection.

This sort of issue usually can't be fixed by CSLA, because it is a UI layer bug. If the UI doesn't tell the business object what it is doing (which would be done by calling one of those methods) then the business layer really can't do anything.

Copyright (c) Marimer LLC