/// <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> /// Removes this object from the chain of events that notify listeners of changes to the content. /// </summary> /// <param name="dependencyObject">The target element that will no longer be registered for state change events.</param> public override void Unregister(DependencyObject dependencyObject) { // This will remove the handler for the mouse focus events. if (dependencyObject is FrameworkElement) { FrameworkElement frameworkElement = dependencyObject as FrameworkElement; frameworkElement.RemoveHandler(FrameworkElement.LostFocusEvent, new RoutedEventHandler(FocusHandler)); } // This will remove the handler for the mouse focus events. if (dependencyObject is FrameworkContentElement) { FrameworkContentElement frameworkContentElement = dependencyObject as FrameworkContentElement; frameworkContentElement.RemoveHandler(FrameworkContentElement.LostFocusEvent, new RoutedEventHandler(FocusHandler)); } }
/// <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); } } }