Browse Source
Merge pull request #3764 from AvaloniaUI/fixes/3551-treeview-clear-index
Update TreeView index when not attached to logical tree.
size-to-content-render-repro
Steven Kirk
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
41 additions and
0 deletions
-
src/Avalonia.Controls/TreeViewItem.cs
-
tests/Avalonia.Controls.UnitTests/TreeViewTests.cs
|
|
|
@ -51,6 +51,7 @@ namespace Avalonia.Controls |
|
|
|
SelectableMixin.Attach<TreeViewItem>(IsSelectedProperty); |
|
|
|
FocusableProperty.OverrideDefaultValue<TreeViewItem>(true); |
|
|
|
ItemsPanelProperty.OverrideDefaultValue<TreeViewItem>(DefaultPanel); |
|
|
|
ParentProperty.Changed.AddClassHandler<TreeViewItem>((o, e) => o.OnParentChanged(e)); |
|
|
|
RequestBringIntoViewEvent.AddClassHandler<TreeViewItem>((x, e) => x.OnRequestBringIntoView(e)); |
|
|
|
} |
|
|
|
|
|
|
|
@ -179,5 +180,16 @@ namespace Avalonia.Controls |
|
|
|
|
|
|
|
return logical != null ? result : @default; |
|
|
|
} |
|
|
|
|
|
|
|
private void OnParentChanged(AvaloniaPropertyChangedEventArgs e) |
|
|
|
{ |
|
|
|
if (!((ILogical)this).IsAttachedToLogicalTree && e.NewValue is null) |
|
|
|
{ |
|
|
|
// If we're not attached to the logical tree, then OnDetachedFromLogicalTree isn't going to be
|
|
|
|
// called when the item is removed. This results in the item not being removed from the index,
|
|
|
|
// causing #3551. In this case, update the index when Parent is changed to null.
|
|
|
|
ItemContainerGenerator.UpdateIndex(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1002,6 +1002,35 @@ namespace Avalonia.Controls.UnitTests |
|
|
|
Assert.Equal(1, child2Node.Presenter.Panel.Children.Count); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Clearing_TreeView_Items_Clears_Index() |
|
|
|
{ |
|
|
|
// Issue #3551
|
|
|
|
var tree = CreateTestTreeData(); |
|
|
|
var target = new TreeView |
|
|
|
{ |
|
|
|
Template = CreateTreeViewTemplate(), |
|
|
|
Items = tree, |
|
|
|
}; |
|
|
|
|
|
|
|
var root = new TestRoot(); |
|
|
|
root.Child = target; |
|
|
|
|
|
|
|
CreateNodeDataTemplate(target); |
|
|
|
ApplyTemplates(target); |
|
|
|
|
|
|
|
var rootNode = tree[0]; |
|
|
|
var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(rootNode); |
|
|
|
|
|
|
|
Assert.NotNull(container); |
|
|
|
|
|
|
|
root.Child = null; |
|
|
|
|
|
|
|
tree.Clear(); |
|
|
|
|
|
|
|
Assert.Empty(target.ItemContainerGenerator.Index.Containers); |
|
|
|
} |
|
|
|
|
|
|
|
private void ApplyTemplates(TreeView tree) |
|
|
|
{ |
|
|
|
tree.ApplyTemplate(); |
|
|
|
|