-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Open
Labels
Description
Describe the bug
A ListBox bound to an observable list with an item already selected will not raise the SelectionChanged event when the observable list is cleared which sends the NotifyCollectionChangedAction.Reset action.
To Reproduce
See failing unit tests here: ptasev@a8f95c3
In particular Handles_Resetting_Items_By_Clearing_With_Existing_Selection
Or use the following in Sandbox app, and fill, select an item, then clear and notice that the event isn't raised, or expected output isn't printed in the debug console.
<PackageReference Include="CommunityToolkit.Mvvm" />
ViewModel.cs
using System.Collections.Generic;
using System.Linq;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Sandbox;
public partial class ViewModel : ObservableObject
{
public AvaloniaList<string> Strings { get; }
public ViewModel()
{
Strings = new AvaloniaList<string>();
}
public void AddRange(IEnumerable<string> items)
{
Strings.AddRange(items);
}
[RelayCommand]
private void Fill()
{
Strings.AddRange(Enumerable.Range(0, 10).Select(x => x.ToString()));
}
[RelayCommand]
private void RemoveLast()
{
Strings.RemoveAt(Strings.Count - 1);
}
[RelayCommand]
private void Clear()
{
Strings.Clear();
}
}MainWindow.axaml.cs
using System.Diagnostics;
using Avalonia.Controls;
namespace Sandbox
{
public partial class MainWindow : Window
{
public ViewModel Vm;
public MainWindow()
{
InitializeComponent();
Vm = new ViewModel();
DataContext = Vm;
}
private void SelectingItemsControl_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
Debug.WriteLine($"{e.AddedItems.Count} {e.RemovedItems.Count}");
}
}
}MainWindow.axaml
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow"
xmlns:sandbox="clr-namespace:Sandbox"
x:DataType="sandbox:ViewModel">
<Grid RowDefinitions="*,*">
<StackPanel>
<Button Command="{Binding FillCommand}" Content="Fill" />
<Button Command="{Binding ClearCommand}" Content="Clear" />
<Button Command="{Binding RemoveLastCommand}" Content="Remove Last" />
</StackPanel>
<ListBox Grid.Row="1" ItemsSource="{Binding Strings}" SelectionChanged="SelectingItemsControl_OnSelectionChanged"></ListBox>
</Grid>
</Window>Expected behavior
The event is raised.
Avalonia version
11.3.12, 12.0.0-preview2
OS
No response
Additional context
I believe TreeView suffers from the same issue.
Reactions are currently unavailable