Browse Source

Merge pull request #3077 from AvaloniaUI/fixes/2980-treeview-autoexpand-multiple-selection

Fix TreeView multiple selection with auto-expand style
pull/3074/head
Steven Kirk 6 years ago
committed by GitHub
parent
commit
1ff1948f9d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/Avalonia.Controls/TreeView.cs
  2. 41
      tests/Avalonia.Controls.UnitTests/TreeViewTests.cs

5
src/Avalonia.Controls/TreeView.cs

@ -1,3 +1,4 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
@ -470,7 +471,7 @@ namespace Avalonia.Controls
if (index > 0)
{
var previous = (TreeViewItem)parentGenerator.ContainerFromIndex(index - 1);
result = previous.IsExpanded ?
result = previous.IsExpanded && previous.ItemCount > 0 ?
(TreeViewItem)previous.ItemContainerGenerator.ContainerFromIndex(previous.ItemCount - 1) :
previous;
}
@ -482,7 +483,7 @@ namespace Avalonia.Controls
break;
case NavigationDirection.Down:
if (from.IsExpanded && intoChildren)
if (from.IsExpanded && intoChildren && from.ItemCount > 0)
{
result = (TreeViewItem)from.ItemContainerGenerator.ContainerFromIndex(0);
}

41
tests/Avalonia.Controls.UnitTests/TreeViewTests.cs

@ -14,6 +14,7 @@ using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Xunit;
@ -892,6 +893,46 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(2, GetItem(target, 0, 1, 0).Level);
}
[Fact]
public void Auto_Expanding_In_Style_Should_Not_Break_Range_Selection()
{
/// Issue #2980.
using (UnitTestApplication.Start(TestServices.RealStyler))
{
var target = new DerivedTreeView
{
Template = CreateTreeViewTemplate(),
SelectionMode = SelectionMode.Multiple,
Items = new List<Node>
{
new Node { Value = "Root1", },
new Node { Value = "Root2", },
},
};
var visualRoot = new TestRoot
{
Styles =
{
new Style(x => x.OfType<TreeViewItem>())
{
Setters =
{
new Setter(TreeViewItem.IsExpandedProperty, true),
},
},
},
Child = target,
};
CreateNodeDataTemplate(target);
ApplyTemplates(target);
_mouse.Click(GetItem(target, 0));
_mouse.Click(GetItem(target, 1), modifiers: InputModifiers.Shift);
}
}
private void ApplyTemplates(TreeView tree)
{
tree.ApplyTemplate();

Loading…
Cancel
Save