Browse Source

Set SelectedIndex/Item on (Un)SelectAll().

pull/2673/head
Steven Kirk 7 years ago
parent
commit
1c85f409b3
  1. 17
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  2. 43
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs

17
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -495,6 +495,8 @@ namespace Avalonia.Controls.Primitives
_selection.Add(i);
}
UpdateSelectedItem(0, false);
foreach (var container in ItemContainerGenerator.Containers)
{
MarkItemSelected(container.Index, true);
@ -507,20 +509,7 @@ namespace Avalonia.Controls.Primitives
/// <summary>
/// Deselects all items in the control.
/// </summary>
protected void UnselectAll()
{
UpdateSelectedItems(() =>
{
_selection.Clear();
foreach (var container in ItemContainerGenerator.Containers)
{
MarkItemSelected(container.Index, false);
}
SelectedItems.Clear();
});
}
protected void UnselectAll() => UpdateSelectedItem(-1);
/// <summary>
/// Updates the selection for an item based on user interaction.

43
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests_Multiple.cs

@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Presenters;
@ -783,6 +784,45 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal(new[] { "Foo", "Bar", "Foo", "Bar" }, target.SelectedItems);
}
[Fact]
public void SelectAll_Sets_SelectedIndex_And_SelectedItem()
{
var target = new TestSelector
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz" },
SelectionMode = SelectionMode.Multiple,
};
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.SelectAll();
Assert.Equal(0, target.SelectedIndex);
Assert.Equal("Foo", target.SelectedItem);
}
[Fact]
public void UnselectAll_Clears_SelectedIndex_And_SelectedItem()
{
var target = new TestSelector
{
Template = Template(),
Items = new[] { "Foo", "Bar", "Baz" },
SelectionMode = SelectionMode.Multiple,
SelectedIndex = 0,
};
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.UnselectAll();
Assert.Equal(-1, target.SelectedIndex);
Assert.Equal(null, target.SelectedItem);
}
[Fact]
public void SelectAll_Handles_Duplicate_Items()
{
@ -836,9 +876,8 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
public new void SelectAll() => base.SelectAll();
public new void UnselectAll() => base.UnselectAll();
public void SelectRange(int index) => UpdateSelection(index, true, true);
public void Toggle(int index) => UpdateSelection(index, true, false, true);
}

Loading…
Cancel
Save