Undo/Redo help

george_yefchak's Avatar


19 Mar, 2018 11:14 PM

Hi folks,

Two questions about undo/redo.

First, do you have an example of how to implement the undo & redo buttons within a menu? My app doesn't use a toolbar control (which is where these would normally go), it only has a simple File menu.

Second, how can I indicate properties which do NOT need to be recorded for undo/redo?


  1. Support Staff 1 Posted by PostSharp Techn... on 20 Mar, 2018 10:18 AM

    PostSharp Technologies's Avatar

    Hello George,

    we don't have this example unfortunately, but we have this documented at the bottom of this page: http://doc.postsharp.net/undoredo-ui

    If you need more help with this, fell free to ask.

    To make a property to not being recorded, you can mark this property using the [NotRecorded] attribute.

    Best regards,

  2. 2 Posted by george_yefchak on 20 Mar, 2018 05:42 PM

    george_yefchak's Avatar

    OK, thanks. But... next problem.

    I have the following property:

            private double _runLengthValue;
            [PostSharp.Patterns.Contracts.Range(0.1, 60.0)]
            public double RunLengthValue //{ get; set; }
                get { return _runLengthValue; }
                    string scopeName = string.Format("Set run length from {0} to {1}", _runLengthValue, value);
                    using (RecordingScope scope = RecordingServices.DefaultRecorder.OpenScope(scopeName))
                        _runLengthValue = value;
    After this property is set, the expected Undo item appears. Once I click on Undo, the previous value appears as expected in the control, but none of the code in my setter is run. So the restored property value doesn't get used. What's wrong here?


  3. Support Staff 3 Posted by PostSharp Techn... on 20 Mar, 2018 08:33 PM

    PostSharp Technologies's Avatar

    This is by design that the property setter is not called during Undo. The changes are recorded at the level of the fields, not properties or methods.


  4. 4 Posted by george_yefchak on 20 Mar, 2018 08:50 PM

    george_yefchak's Avatar

    I’m not sure how this is useful. I’ll need to implement undo myself without using PostSharp. :-(


    Sent from George's iPhone

  5. Support Staff 5 Posted by PostSharp Techn... on 21 Mar, 2018 08:29 AM

    PostSharp Technologies's Avatar

    By design, the recorder records changes in the state, which is fully determined by the value of fields and collections (in .NET, the only two "things" that can store state on the heap are fields and array cells, so this is what we record). This approach allows to reverse to a previous state of the object independently from the logic that sets this state. For instance some fields may be set from methods (not property setters) and in this case there is no way to call the "reverse" method.

    If you need to execute custom logic during undo or redo, you have to implement the IRecordableCallback interface.

    Another approach, if you need to mix recording of changes in the heap state with recording of something else (such as a file system or database operation) requiring specific logic, is to create custom commands by implementing the Operation class and adding them manually to the recorder using Recorder.AddOperation.

    I hope this helps.


  6. 6 Posted by george_yefchak on 21 Mar, 2018 05:23 PM

    george_yefchak's Avatar


    The IRecordableCallback interface doesn't include a way to see which state change is affected (and thus which property change should be triggered). I'd need to track all the myself, which isn't much easier than writing the whole undo/redo logic.


  7. Support Staff 7 Posted by PostSharp Techn... on 22 Mar, 2018 08:06 AM

    PostSharp Technologies's Avatar

    Hi George,

    If all you need is to trigger the PropertyChanged event, then you can combine the [Recordable] and [NotifyPropertyChanged] aspects. They both work well together, as [NotifyPropertyChanged] also works at the level of fields.


  8. Support Staff 8 Posted by PostSharp Techn... on 11 Apr, 2018 03:19 PM

    PostSharp Technologies's Avatar


    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    PostSharp Team

  9. PostSharp Technologies closed this discussion on 11 Apr, 2018 03:19 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac