Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to undo the command during ItemModified event ? (Read 396 times)
Mandy
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 47
Joined: Jun 3rd, 2020
How to undo the command during ItemModified event ?
Oct 23rd, 2023 at 5:17am
Print Post  
Hi,

I want to undo the change command when db update failed.

Code
Select All
 private void Calendar_ItemModified(object sender, ItemModifiedEventArgs e)
        {
	   var item = e.Item;
            var executeUndo = false;

            MindFusion.Scheduling.Item appChangeItem = item;
            MindFusion.Scheduling.Schedule appSchedule = this.calendar.Schedule;
            MindFusion.Scheduling.ChangeItemCommand appChangeCommand = null;

            this.calendar.BeginInit();
            appSchedule.UndoEnabled = true;
            appSchedule.StartCompositeOperation();
            appChangeCommand = new MindFusion.Scheduling.ChangeItemCommand(appSchedule, appChangeItem);

            //I want to undo the change command when db update failed.
            executeUndo = true;

            appSchedule.ExecuteCommand(appChangeCommand);
            appSchedule.CommitCompositeOperation();
            if (executeUndo)
            {
                appSchedule.Undo();
            }
            appSchedule.UndoEnabled = false;
            this.calendar.EndInit();
        }
 

  
Back to top
 
IP Logged
 
Slavcho
God Member
*****
Offline


tech.support

Posts: 3154
Joined: Oct 19th, 2005
Re: How to undo the command during ItemModified event ?
Reply #1 - Oct 23rd, 2023 at 8:21am
Print Post  
Hi Mandy,

From this code it seems you are enabling undo only in ItemModified event handler, and at that point item's original times have already changed and old values discarded. If you keep UndoEnabled set from the start of the application, the control would record a ChangeItemCommand automatically anyway just before raising ItemModified event - then the handler could just call Undo when it detects errors in DB code.

Alternatively, if you don't want to rely on undo for that, you could record item's original times from ItemStartModifying event handler, and restore them from ItemModified if there's DB error.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
God Member
*****
Offline


tech.support

Posts: 3154
Joined: Oct 19th, 2005
Re: How to undo the command during ItemModified event ?
Reply #2 - Oct 23rd, 2023 at 8:44am
Print Post  
Now I'm seeing ItemModifiedEventArgs also reports OldStartTime and OldEndTime, so instead of undoing from ItemModified, you could assign the old values back to the item.
  
Back to top
 
IP Logged
 
Mandy
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 47
Joined: Jun 3rd, 2020
Re: How to undo the command during ItemModified event ?
Reply #3 - Oct 24th, 2023 at 7:36am
Print Post  
Hi Slavcho,

I already set UndoEnabled = true at the beginning.

1)Click "Create" button to create a recurring appointment.
2)Checked the "Undo" checkbox.
3)Drag the item of 10/9 to 10/11.

The ItemModified event should be restored the items to 10/9, but now all items have been cleared.
  

Scheduling_ICalendarFormat_001.zip ( 374 KB | 24 Downloads )
Back to top
 
IP Logged
 
Slavcho
God Member
*****
Offline


tech.support

Posts: 3154
Joined: Oct 19th, 2005
Re: How to undo the command during ItemModified event ?
Reply #4 - Oct 25th, 2023 at 4:47am
Print Post  
Hi Mandy,

Calendar's UndoManager is still recording an internal composite command for the change at this time and won't allow immediate undo. You could use InvokeAsync to undo after the mouse event handler:
Code
Select All
Dispatcher.InvokeAsync(() => calendar.Schedule.Undo()); 



Or otherwise reset back to the Old*Time values provided as event arguments.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint