/// <summary> /// Handles a change to the undo manager for a given dependency object. /// </summary> /// <param name="dependencyObject">The target object for this attached property.</param> /// <param name="dependencyPropertyChangedEventArgs">The event parameters.</param> private static void OnUndoScopeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { // This will remove the high level handlers for the root node in the UndoManager scope. if (dependencyPropertyChangedEventArgs.OldValue is UndoManager) { // This is the manager that was previously associated with the root of the Undo Scope. UndoManager undoManager = dependencyPropertyChangedEventArgs.OldValue as UndoManager; // This will remove a handler for the mouse focus events. undoManager.focusUndo.Unregister(dependencyObject); // This will remove the UndoManager from all the child controls. dependencyObject.ClearValue(UndoManager.UndoManagerProperty); // This will remove the handlers for the undo/redo operations for the given undo manager. if (dependencyObject is FrameworkElement) { FrameworkElement frameworkElement = dependencyObject as FrameworkElement; frameworkElement.RemoveHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(undoManager.OnCommand)); } //// This will remove the handlers for the undo/redo operations for the given undo manager. if (dependencyObject is FrameworkContentElement) { FrameworkContentElement frameworkContentElement = dependencyObject as FrameworkContentElement; frameworkContentElement.RemoveHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(undoManager.OnCommand)); } } // This will add the high level handlers for the root node in the UndoManager scope. if (dependencyPropertyChangedEventArgs.NewValue is UndoManager) { // This is the manager that will be used for this Undo Scope to manage the Undo and Redo operations. UndoManager undoManager = dependencyPropertyChangedEventArgs.NewValue as UndoManager; // This will add a handler for the mouse focus events. undoManager.focusUndo.Register(dependencyObject); // This adds the Undo Manager to all the child windows in the scope of this user interface element which forms // the root of the scope. dependencyObject.SetValue(UndoManager.UndoManagerProperty, undoManager); // This will add the handlers for the undo/redo operations for the given undo manager. if (dependencyObject is FrameworkElement) { FrameworkElement frameworkElement = dependencyObject as FrameworkElement; frameworkElement.AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(undoManager.OnCommand), true); } //// This will add the handlers for the undo/redo operations for the given undo manager. if (dependencyObject is FrameworkContentElement) { FrameworkContentElement frameworkContentElement = dependencyObject as FrameworkContentElement; frameworkContentElement.AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(undoManager.OnCommand), true); } } }
/// <summary> /// Registers this object to receive notifications of changes to the content. /// </summary> /// <param name="dependencyObject">The target element that will register for state change events.</param> public override void Register(DependencyObject dependencyObject) { // The Undo/Redo strategy involves handling global operations that are common to all controls and specific operations // that are particular to a family of controls. This registers handlers for this family of controls that will add the // proper actions to the Undo/Redo stacks. if (dependencyObject is FrameworkElement) { FrameworkElement frameworkElement = dependencyObject as FrameworkElement; frameworkElement.AddHandler(FrameworkElement.LostFocusEvent, new RoutedEventHandler(FocusHandler), true); } // The Undo/Redo strategy involves handling global operations that are common to all controls and specific operations // that are particular to a family of controls. This registers handlers for this family of controls that will add the // proper actions to the Undo/Redo stacks. if (dependencyObject is FrameworkContentElement) { FrameworkContentElement frameworkContentElement = dependencyObject as FrameworkContentElement; frameworkContentElement.AddHandler(FrameworkContentElement.LostFocusEvent, new RoutedEventHandler(FocusHandler), true); } }
/// <summary> /// Handles a change to the undo manager for a given dependency object. /// </summary> /// <param name="dependencyObject">The target object for this attached property.</param> /// <param name="dependencyPropertyChangedEventArgs">The event parameters.</param> static void OnUndoScopeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { // Validate the parameters. if (dependencyObject == null) { throw new ArgumentNullException("dependencyObject"); } if (dependencyPropertyChangedEventArgs == null) { throw new ArgumentNullException("dependencyPropertyChangedEventArgs"); } // Extract the specific arguments from the generic ones. UndoManager oldUndoManager = dependencyPropertyChangedEventArgs.OldValue as UndoManager; UndoManager newUndoManager = dependencyPropertyChangedEventArgs.NewValue as UndoManager; FrameworkElement frameworkElement = dependencyObject as FrameworkElement; FrameworkContentElement frameworkContentElement = dependencyObject as FrameworkContentElement; // This is the manager that was previously associated with the root of the Undo Scope. if (oldUndoManager != null) { // This will remove a handler for the mouse focus events. oldUndoManager.focusUndo.Unregister(dependencyObject); // This will remove the UndoManager from all the child controls. dependencyObject.ClearValue(UndoManager.UndoManagerProperty); // This will remove the handlers for the undo/redo operations for the given undo manager. if (frameworkElement != null) { frameworkElement.RemoveHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(oldUndoManager.OnCommand)); } // This will remove the handlers for the undo/redo operations for the given undo manager. if (frameworkContentElement != null) { frameworkContentElement.RemoveHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(oldUndoManager.OnCommand)); } } // This will add the high level handlers for the root node in the UndoManager scope. if (newUndoManager != null) { // This will add a handler for the mouse focus events. newUndoManager.focusUndo.Register(dependencyObject); // This adds the Undo Manager to all the child windows in the scope of this user interface element which forms // the root of the scope. dependencyObject.SetValue(UndoManager.UndoManagerProperty, newUndoManager); // This will add the handlers for the undo/redo operations for the given undo manager. if (frameworkElement != null) { frameworkElement.AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(newUndoManager.OnCommand), true); } //// This will add the handlers for the undo/redo operations for the given undo manager. if (frameworkContentElement != null) { frameworkContentElement.AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(newUndoManager.OnCommand), true); } } }