Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where the ActiveContent binding doesn't update two ways when removing a document. #205

Merged
merged 1 commit into from
Nov 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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