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

Selection Model Issues #4496

Closed
danwalmsley opened this issue Aug 17, 2020 · 1 comment · Fixed by #4533
Closed

Selection Model Issues #4496

danwalmsley opened this issue Aug 17, 2020 · 1 comment · Fixed by #4533

Comments

@danwalmsley
Copy link
Member

danwalmsley commented Aug 17, 2020

  • Always selected mode - Add 3 items, select the last item... remove the middle item... this leaves no item selected.
  • Crash when removing all items, and then inserting at position 0.
    image

-- more to come.

grokys added a commit that referenced this issue Aug 17, 2020
grokys added a commit that referenced this issue Aug 17, 2020
@grokys
Copy link
Member

grokys commented Aug 17, 2020

Both of these issues are related but the second is the most interesting (and hardest to fix). The following unit test demonstrates the problem:

[Fact]
public void Binding_SelectedIndex_Does_Not_Cause_ArgumentOutOfRange()
{
    // Issue #4496 (part 2)
    var items = new ObservableCollection<string>();

    var other = new ListBox
    {
        Template = Template(),
        Items = items,
        SelectionMode = SelectionMode.AlwaysSelected,
    };

    var target = new ListBox
    {
        Template = Template(),
        Items = items,
        [!ListBox.SelectedIndexProperty] = other[!ListBox.SelectedIndexProperty],
    };

    target.ApplyTemplate();
    target.Presenter.ApplyTemplate();
    other.ApplyTemplate();
    other.Presenter.ApplyTemplate();

    items.Add("Foo");
}

What happens is:

  • target has SelectedIndex bound to other.SelectedIndex
  • An item is added: other auto-selects the item
  • The binding takes effect, setting target.SelectedIndex = 0
  • target's selection model recieves CollectionChanged, saying that an item was inserted at index 0
  • So it adjusts the index of the selected item by adding 1 to it, to an index that doesn't exist

grokys added a commit that referenced this issue Aug 19, 2020
@grokys grokys mentioned this issue Aug 20, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants