Skip to content

Commit

Permalink
Fix issue where the ActiveContent binding doesn't update two ways whe…
Browse files Browse the repository at this point in the history
…n removing a document.
  • Loading branch information
PatrickHofman committed Oct 29, 2020
1 parent 2ecba2d commit 3df1d0f
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions source/Components/AvalonDock/DockingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LayoutDocument>())
{
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<LayoutDocument>())
{
if (layoutDocument == documentToFind)
{
return index;
}

index++;
}

//
// Not found.
//
return -1;
}

internal void ExecuteCloseAllButThisCommand(LayoutContent contentSelected)
Expand Down

0 comments on commit 3df1d0f

Please sign in to comment.