Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion src/Avalonia.Controls/Generators/TreeContainerIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public class TreeContainerIndex
/// <summary>
/// Gets the currently materialized containers.
/// </summary>
public IEnumerable<IControl> Items => _containerToItem.Keys;
public IEnumerable<IControl> Containers => _containerToItem.Keys;

/// <summary>
/// Gets the items of currently materialized containers.
/// </summary>
public IEnumerable<object> Objects => _containerToItem.Values;

/// <summary>
/// Adds an entry to the index.
Expand Down
22 changes: 22 additions & 0 deletions src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Avalonia.Controls.Generators;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Styling;
using Avalonia.VisualTree;
Expand Down Expand Up @@ -459,6 +460,27 @@ protected override void OnDataContextEndUpdate()
}
}

/// <summary>
/// Handles 'select all' (Ctrl+A) in <see cref="SelectingItemsControl"/>.
/// </summary>
/// <param name="e">The key events.</param>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);

if (!e.Handled)
{
var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e));

if (this.SelectionMode == SelectionMode.Multiple && Match(keymap.SelectAll))
{
SynchronizeItems(SelectedItems, Items?.Cast<object>());
e.Handled = true;
}
}
}

/// <summary>
/// Moves the selection in the specified direction relative to the current selection.
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion src/Avalonia.Controls/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Avalonia.Controls.Generators;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;
Expand Down Expand Up @@ -244,7 +245,7 @@ private void SelectedItemsCollectionChanged(object sender, NotifyCollectionChang
break;
case NotifyCollectionChangedAction.Reset:

foreach (IControl container in ItemContainerGenerator.Index.Items)
foreach (IControl container in ItemContainerGenerator.Index.Containers)
{
MarkContainerSelected(container, false);
}
Expand Down Expand Up @@ -402,6 +403,18 @@ protected override void OnKeyDown(KeyEventArgs e)
SelectedItem = ElementAt(Items, 0);
}
}

if (!e.Handled)
{
var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e));

if (this.SelectionMode == SelectionMode.Multiple && Match(keymap.SelectAll))
{
SelectingItemsControl.SynchronizeItems(SelectedItems, ItemContainerGenerator.Index.Objects);
e.Handled = true;
}
}
}

private TreeViewItem GetContainerInDirection(
Expand Down
6 changes: 3 additions & 3 deletions tests/Avalonia.Controls.UnitTests/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Items_Should_Be_Created_Using_ItemTemplate_If_Present()

ApplyTemplates(target);

var items = target.ItemContainerGenerator.Index.Items
var items = target.ItemContainerGenerator.Index.Containers
.OfType<TreeViewItem>()
.ToList();

Expand Down Expand Up @@ -463,11 +463,11 @@ public void Removing_Item_Should_Remove_Itself_And_Children_From_Index()
CreateNodeDataTemplate(target);
ApplyTemplates(target);

Assert.Equal(5, target.ItemContainerGenerator.Index.Items.Count());
Assert.Equal(5, target.ItemContainerGenerator.Index.Containers.Count());

tree[0].Children.RemoveAt(1);

Assert.Equal(3, target.ItemContainerGenerator.Index.Items.Count());
Assert.Equal(3, target.ItemContainerGenerator.Index.Containers.Count());
}

[Fact]
Expand Down