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

Hackfix for calling SelectAll with SelectedItem binding #13868

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions src/Avalonia.Controls/Selection/SelectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public int SelectedIndex
get => _selectedIndex;
set
{
if (_operation is not null && _operation.UpdateCount == 0)
{
// An operation is in the process of being committed. In this case, if the new
// value for SelectedIndex is unchanged then we need to ignore it. It could be
// the result of a two-way binding to SelectedIndex writing back to the
// property. The binding system should really be fixed to ensure that it's not
// writing back the same value, but this is a workaround until the binding
// refactor is complete. See #13676.
if (value == _selectedIndex)
return;
}

using var update = BatchUpdate();
Clear();
Select(value);
Expand Down Expand Up @@ -675,8 +687,6 @@ private void CommitOperation(Operation operation, bool raisePropertyChanged = tr
}
}



if (raisePropertyChanged)
{
if (oldSelectedIndex != _selectedIndex)
Expand Down
59 changes: 59 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ListBoxTests_Multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Styling;
Expand Down Expand Up @@ -577,6 +578,64 @@ public void Shift_Down_Key_Selecting_Selects_Range_End_From_Focus_Moved_With_Ctr
Assert.True(target.ContainerFromIndex(3).IsFocused);
}

[Fact]
public void SelectAll_Works_From_No_Selection_When_SelectedItem_Is_Bound_TwoWay()
{
// Issue #13676
using var app = UnitTestApplication.Start(TestServices.RealFocus);
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
ItemsSource = new[] { "Foo", "Bar", "Baz", "Qux" },
SelectionMode = SelectionMode.Multiple,
Width = 100,
Height = 100,
};

var root = new TestRoot(target);
root.LayoutManager.ExecuteInitialLayoutPass();

target.Bind(ListBox.SelectedItemProperty, new Binding("Tag")
{
Mode = BindingMode.TwoWay,
RelativeSource = new RelativeSource(RelativeSourceMode.Self),
});

target.SelectAll();

Assert.Equal(new[] { 0, 1, 2, 3 }, target.Selection.SelectedIndexes);
Assert.Equal(new[] { "Foo", "Bar", "Baz", "Qux" }, target.SelectedItems);
}

[Fact]
public void SelectAll_Works_From_No_Selection_When_SelectedIndex_Is_Bound_TwoWay()
{
// Issue #13676
using var app = UnitTestApplication.Start(TestServices.RealFocus);
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
ItemsSource = new[] { "Foo", "Bar", "Baz", "Qux" },
SelectionMode = SelectionMode.Multiple,
Width = 100,
Height = 100,
};

var root = new TestRoot(target);
root.LayoutManager.ExecuteInitialLayoutPass();

target.Bind(ListBox.SelectedIndexProperty, new Binding("Tag")
{
Mode = BindingMode.TwoWay,
RelativeSource = new RelativeSource(RelativeSourceMode.Self),
});

target.SelectAll();

Assert.Equal(new[] { 0, 1, 2, 3 }, target.Selection.SelectedIndexes);
Assert.Equal(new[] { "Foo", "Bar", "Baz", "Qux" }, target.SelectedItems);
}

private Control CreateListBoxTemplate(TemplatedControl parent, INameScope scope)
{
return new ScrollViewer
Expand Down