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 with Virtualization="None" selected pseudoclass incorrect after move #2868

Closed
aguahombre opened this issue Aug 22, 2019 · 5 comments · Fixed by #9677
Closed

ListBox with Virtualization="None" selected pseudoclass incorrect after move #2868

aguahombre opened this issue Aug 22, 2019 · 5 comments · Fixed by #9677

Comments

@aguahombre
Copy link
Contributor

aguahombre commented Aug 22, 2019

Using build 0.8.999-cibuild0003548-beta.
Calling the Move method of an ObservableCollection bound to a ListBox with VirtualizationMode="None" causes the ListBox's selected items to go crazy.

Here is a repo:
Initial selection is A (index 0).
image

After pressing the Move button several times, the selected pseudoclass is present on multiple items.
image

<Window 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"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaApplication11.MainWindow"
        Title="AvaloniaApplication11">
  <Window.Styles>
    <Style Selector="ListBoxItem:selected /template/ ContentPresenter">
      <Setter Property="ContentTemplate">
        <DataTemplate>
          <Border BorderBrush="Crimson" BorderThickness="2" >
            <TextBlock Text="{Binding}"/>
          </Border>
        </DataTemplate>
      </Setter>    
    </Style>
  </Window.Styles>
  <Grid RowDefinitions="Auto,*">
    <Grid ColumnDefinitions="*,*,*">
      <Button Grid.Column="0" Content="Move" Command="{Binding Move}"/>
      <TextBlock Grid.Column="1" Text="{Binding #listbox.SelectedIndex, StringFormat=SelectedIndex \{0\}}"/>
      <TextBlock Grid.Column="2" Text="{Binding #listbox.SelectedItem, StringFormat=SelectedItem \{0\}}"/>
    </Grid>
    <ListBox Grid.Row="1" Name="listbox" SelectedIndex="0" Items="{Binding Items}" VirtualizationMode="None" />
  </Grid>
</Window>
using System.Collections.ObjectModel;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace AvaloniaApplication11
{
    public class MainWindow : Window
    {
        private ListBox _listbox;
        bool _down;

        public ObservableCollection<string> Items { get; } = new ObservableCollection<string>(new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", });
        public MainWindow()
        {
            DataContext = this;
            AvaloniaXamlLoader.Load(this);

            _listbox = this.FindControl<ListBox>("listbox");
        }

        public void Move()
        {
            if (_listbox.SelectedIndex < 0)
            {
                _listbox.SelectedIndex = 0;
            }

            if (_down)
            {
                if (_listbox.SelectedIndex < Items.Count - 1)
                {
                    Items.Move(_listbox.SelectedIndex, _listbox.SelectedIndex + 1);
                }
                else
                {
                    _down = false;
                    Items.Move(_listbox.SelectedIndex, _listbox.SelectedIndex - 1);
                }
            }
            else
            {
                if (_listbox.SelectedIndex > 0)
                {
                    Items.Move(_listbox.SelectedIndex, _listbox.SelectedIndex - 1);
                }
                else
                {
                    _down = true;
                    Items.Move(_listbox.SelectedIndex, _listbox.SelectedIndex + 1);
                }
            }
        }
    }
}

@aguahombre aguahombre changed the title ListBox SelectedIndex and SelectedItem inconsistent after move ListBox with Virtualization="None" selected pseudoclass incorrect after move Aug 22, 2019
@danwalmsley
Copy link
Member

@aguahombre we recently made a fix to resolve a similar issue, can you confirm if this is still an issue?

@aguahombre
Copy link
Contributor Author

No the problem is not fixed but has changed in 0.8.999-cibuild0004187-beta

Starting with A selected
image

If I click the Move button which calls ObservableCollection Move method to move A to index 1, I get the following output
image

As you can see, both A and C are shown as selected, whereas only A should be selected.

@aguahombre aguahombre reopened this Oct 9, 2019
@aguahombre
Copy link
Contributor Author

This is still an issue in 0.8.999.
See new incorrect behaviour above.
Must have closed this by accident!

@grokys
Copy link
Member

grokys commented Jan 26, 2020

@aguahombre for 0.10 i'm hoping to use ItemsRepeater to handle our ListBox virtualization, which should fix this. However that means that it's unlikely to be fixed in the near term if there's a workaround.

Would you be able to do a remove/insert instead of a move as a workaround?

@aguahombre
Copy link
Contributor Author

Thanks, will use workaround for now

@grokys grokys added this to Selection in @grokys todos Feb 17, 2020
@grokys grokys added this to ItemsControl in @grokys todos Dec 2, 2022
@grokys todos automation moved this from ItemsControl to Done Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
@grokys todos
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants