Created
November 7, 2018 10:45
-
-
Save Larry57/92b22c71a114dbcd2f8427f59e1fd1dd to your computer and use it in GitHub Desktop.
Awsh : undo manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace OneFileTools { | |
/// <summary> | |
/// Awsh Undo helper | |
/// </summary> | |
public class Awsh { | |
Stack<Action> undoActions = new Stack<Action>(); | |
bool canUndo; | |
public event EventHandler CanUndoChanged; | |
public event Action BeforeUndo; | |
public event Action AfterUndo; | |
public event Action UndoActionRegistered; | |
public bool CanUndo { | |
get { | |
return canUndo; | |
} | |
set { | |
if(canUndo != value) { | |
canUndo = value; | |
CanUndoChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} | |
} | |
public void RegisterUndoAction(Action undoAction) { | |
undoActions.Push(undoAction); | |
CanUndo = undoActions.Any(); | |
UndoActionRegistered?.Invoke(); | |
} | |
public bool IsUndoing { | |
get; | |
private set; | |
} | |
public void Undo() { | |
IsUndoing = true; | |
BeforeUndo?.Invoke(); | |
var action = undoActions.Pop(); | |
action.Invoke(); | |
AfterUndo?.Invoke(); | |
IsUndoing = false; | |
CanUndo = undoActions.Any(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DevExpress.Xpo; | |
using DevExpress.XtraGrid.Views.Grid; | |
using DevExpress.XtraVerticalGrid; | |
using System; | |
using System.Linq; | |
using System.Windows.Forms; | |
namespace OneFileTools { | |
public static class AwshExtensions { | |
public static void RegisterUndo(this Binding binding, Awsh awsh) { | |
binding.Parse += (s, e) => { | |
if(awsh.IsUndoing) | |
return; | |
var b = s as Binding; | |
var obj = b.DataSource; | |
var propertyInfo = obj.GetType().GetProperty(b.BindingMemberInfo.BindingField); | |
var oldValue = propertyInfo.GetValue(obj); | |
if(!object.Equals(oldValue, e.Value)) { | |
awsh.RegisterUndoAction(() => propertyInfo.SetValue(obj, oldValue)); | |
} | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment