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

ListBox deselects selected item when calling Move() on bound ObservableCollection #16279

Open
doobah opened this issue Jul 11, 2024 · 1 comment
Labels

Comments

@doobah
Copy link

doobah commented Jul 11, 2024

Bug Description

When a ListBox.ItemSource is bound to an ObservableCollection, and an item is selected, if Move() is called on the underlying item in the collection, then the ListBox selection is cleared. This does not happen if the selected item is not the one that Move() is called on, even though its position may change.

To Reproduce

The following example contains a ListBox bound to an ObservableCollection. The button calls Move() to move the first item to the end of the list. Note that when the first item is selected and subsequently moved, it is deselected. Selecting any other item and calling Move() causes the item's position to change, but it stays selected.

XAML:

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:Bug.ViewModels"
             mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450"
             Width="200" Height="450"
             x:Class="Bug.Views.MainView"
             x:DataType="vm:MainViewModel">
  <DockPanel>
    <Button Content="Move" DockPanel.Dock="Right" Command="{Binding MoveCommand}"/>
    <ListBox ItemsSource="{Binding Items}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Label Content="{Binding}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </DockPanel>
</UserControl>

ViewModel:

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<string> Items { get; set; } = new ObservableCollection<string>()
    {
        "Item 1",
        "Item 2",
        "Item 3",
        "Item 4",
        "Item 5"
    };

    public ICommand MoveCommand { get; set; }

    public MainViewModel()
    {
        MoveCommand = ReactiveCommand.Create(() =>
        {
            Items.Move(0, Items.Count - 1);
        });
    }
}

Expected Behavior

The ListBox's selected item should remain selected after calling ObservableCollection.Move() on the underlying item.

Avalonia Version

11.1.0-rc2

OS

Windows

@doobah doobah added the bug label Jul 11, 2024
@doobah doobah changed the title ListBox deselects selected item when calling Move() on underlying ObservableCollection item ListBox deselects selected item when calling Move() on bound ObservableCollection Jul 12, 2024
@doobah
Copy link
Author

doobah commented Sep 14, 2024

This also happens with sorting via a Dynamic Data source cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant