0

I'm currently using CSLA 4.5 and am running into an issue where I get an error

Edit level mismatch in CopyState

What's strange is that if I execute a BeginEdit() call on the business object in one section of code, it has no problem, but if I place it inside of my popup window logic, it throws this exception, and I'm not sure why. The key part that is weird is that it is the SAME OBJECT:

MainControl.xaml.cs:

public void ShowPopup()
{
    // ...

    var target = ...;
    //target.BeginEdit(); <== No problem if I uncomment this

    var window = new PopupWindow
                 {
                     DataContext = new ConfirmationViewModel(target),
                     IsApplyCancel = true,
                     Owner = this,
                 };

    window.Show();
}

PopuWindow.xaml.cs:

public class PopupWindow : Window
{
    private static void OnIsApplyCancelChanged(object sender, DependencyPropertyChangedEventArgs args)
    {
        var window = sender as Window;

        if (window != null)
        {
            var context = window.DataContext as CSLAViewModelBase;

            if (context != null)
            {
                var target = context.Target;

                target.BeginEdit(); // EXCEPTION!!!
            }
        }
    }
}

ConfirmationViewModel.cs:

public class ConfirmationViewModel : CSLAViewModelBase
{
     public ConfirmationViewModel(BusinessBase target)
         : base(target) { ... }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
michael
  • 14,844
  • 28
  • 89
  • 177

1 Answers1

0

Databinding UIs typically look for IEditableObject and will call it for you automatically when the object is bound to the form. That interface doesn't know about n level undo though so if Csla detects multiple calls to those methods it throws an exception. You probably don't need the BeginEdit in your dialog and only need to worry about Cancel or EndEdit after you've unbound the object from the form.

The ViewModelBase also does this for you I believe.

Andy
  • 8,432
  • 6
  • 38
  • 76