From 11043c9ea9a28b15b720557a8bd70c431d7658f4 Mon Sep 17 00:00:00 2001 From: Noisrev Date: Mon, 26 Dec 2022 15:11:22 +0800 Subject: [PATCH] Add a Deactivated event handler in the Navigator Window - Close the window when it is not active and activate the selected content --- .../AvalonDock/Controls/NavigatorWindow.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/source/Components/AvalonDock/Controls/NavigatorWindow.cs b/source/Components/AvalonDock/Controls/NavigatorWindow.cs index ccfa3562..7deee8ca 100644 --- a/source/Components/AvalonDock/Controls/NavigatorWindow.cs +++ b/source/Components/AvalonDock/Controls/NavigatorWindow.cs @@ -98,6 +98,7 @@ internal NavigatorWindow(DockingManager manager) DataContext = this; Loaded += OnLoaded; Unloaded += OnUnloaded; + Deactivated += OnDeactivated; UpdateThemeResources(); } @@ -380,11 +381,7 @@ protected override void OnKeyUp(KeyEventArgs e) { if (!(e.Key == Key.Tab || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)) { - Close(); - if (SelectedDocument != null && SelectedDocument.ActivateCommand.CanExecute(null)) - SelectedDocument.ActivateCommand.Execute(null); - if (SelectedDocument == null && SelectedAnchorable != null && SelectedAnchorable.ActivateCommand.CanExecute(null)) - SelectedAnchorable.ActivateCommand.Execute(null); + CloseAndActiveSelected(); e.Handled = true; } base.OnKeyUp(e); @@ -520,6 +517,21 @@ private void OnLoaded(object sender, RoutedEventArgs e) private void OnUnloaded(object sender, RoutedEventArgs e) => Unloaded -= OnUnloaded; + private void OnDeactivated(object sender, EventArgs e) + { + CloseAndActiveSelected(); + } + + private void CloseAndActiveSelected() + { + Deactivated -= OnDeactivated; + Close(); + if (SelectedDocument != null && SelectedDocument.ActivateCommand.CanExecute(null)) + SelectedDocument.ActivateCommand.Execute(null); + if (SelectedDocument == null && SelectedAnchorable != null && SelectedAnchorable.ActivateCommand.CanExecute(null)) + SelectedAnchorable.ActivateCommand.Execute(null); + } + private void FocusSelectedItem(ListBox list) { if (list.SelectedIndex >= 0)