From 3df1d0fbdbb279fd0cfc9ea9cc8d203a9df70d5d Mon Sep 17 00:00:00 2001 From: Patrick Hofman Date: Thu, 29 Oct 2020 14:35:45 +0100 Subject: [PATCH] Fix issue where the ActiveContent binding doesn't update two ways when removing a document. --- .../Components/AvalonDock/DockingManager.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/source/Components/AvalonDock/DockingManager.cs b/source/Components/AvalonDock/DockingManager.cs index f0837ca9..e8bd207b 100644 --- a/source/Components/AvalonDock/DockingManager.cs +++ b/source/Components/AvalonDock/DockingManager.cs @@ -1718,11 +1718,83 @@ internal void ExecuteCloseCommand(LayoutDocument document) DocumentClosing(this, argsClosing); if (argsClosing.Cancel) return; } + + // + // Determine the index of the document that will be removed. + // + int indexOfDocumentToRemove = GetIndexOfDocument(document); + if (!document.CloseDocument()) return; + RemoveViewFromLogicalChild(document); if (document.Content is UIElement uIElement) RemoveLogicalChild(uIElement); DocumentClosed?.Invoke(this, new DocumentClosedEventArgs(document)); + + int indexOfDocumentToSelect = indexOfDocumentToRemove - 1; + + if (indexOfDocumentToSelect < 0) + { + indexOfDocumentToSelect = 0; + } + + // + // Determine the new active document and activate it. + // This doesn't only update the layout, but also all related (dependency) properties. + // + LayoutDocument layoutDocument = GetDocumentOnIndex(indexOfDocumentToSelect); + + if (layoutDocument != null) + { + layoutDocument.IsActive = true; + } + } + + private LayoutDocument GetDocumentOnIndex(int indexToFind) + { + if (indexToFind < 0) + { + throw new ArgumentOutOfRangeException(nameof(indexToFind)); + } + + int index = 0; + + foreach (LayoutDocument layoutDocument in this.Layout.Descendents().OfType()) + { + if (index == indexToFind) + { + return layoutDocument; + } + + index++; + } + + return null; + } + + private int GetIndexOfDocument(LayoutDocument documentToFind) + { + if (documentToFind == null) + { + throw new ArgumentNullException(nameof(documentToFind)); + } + + int index = 0; + + foreach (LayoutDocument layoutDocument in this.Layout.Descendents().OfType()) + { + if (layoutDocument == documentToFind) + { + return index; + } + + index++; + } + + // + // Not found. + // + return -1; } internal void ExecuteCloseAllButThisCommand(LayoutContent contentSelected)