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

Unable to bind to SelectionModel.SelectedItems or SelectedIndexes #15497

Closed
grokys opened this issue Apr 24, 2024 · 0 comments · Fixed by #15498
Closed

Unable to bind to SelectionModel.SelectedItems or SelectedIndexes #15497

grokys opened this issue Apr 24, 2024 · 0 comments · Fixed by #15498
Labels
bug customer-priority Issue reported by a customer with a support agreement.

Comments

@grokys
Copy link
Member

grokys commented Apr 24, 2024

Describe the bug

Reported by a customer:

Binding ItemsControl.ItemsSource to SelectionModel.SelectedItems or SelectionModel.SelectedIndexes does not work.

SelectionModel raises a PropertyChanged event on SelectedItems and SelectedIndexes, but because the instance of the collection hasn't changed, property system doesn't consider the value to have changed, so consequently the bound ItemsControl doesn't register the change. This behaviour comes from WinUI which was the original source of our SelectionModel (though they have diverged significantly since the initial port):

https://github.com/microsoft/microsoft-ui-xaml/blob/winui3/release/1.4-stable/controls/dev/Repeater/SelectionModel.cpp#L591-L592

I assume that the WinUI SelectionModel was written without the expectation that someone would bind to the selected collections on a SelectionModel.

To Reproduce

<Grid ColumnDefinitions="*,*">
    <ListBox Name="lb" ItemsSource="{Binding}" SelectionMode="Multiple"/>
    <ListBox Grid.Column="1" ItemsSource="{Binding #lb.Selection.SelectedIndexes}"/>
</Grid>

Expected behavior

SelectionModel.SelectedItems or SelectionModel.SelectedIndexes and should follow the usual semantics for property and collection changes, not the strange semantics we have inherited from WinUI.

Avalonia version

all

OS

No response

Additional context

No response

@grokys grokys added bug customer-priority Issue reported by a customer with a support agreement. labels Apr 24, 2024
grokys added a commit that referenced this issue Apr 24, 2024
Make `SelectionModel.SelectedItems` and `SelectionModel.SelectedIndexes` implement `INotifyCollectionChanged` so that they can be bound to.

As well as implementing `INotifyCollectionChanged` on the collections, we also had to implement `IList` (see #8764) so refactored this out into a base class.

For the sake of simplicity, these collections only raise `Reset` for any change: this is may need to be changed later but I'd rather follow the KISS principle for the moment until something more complex is proven necessary.

Fixes #15497
maxkatz6 pushed a commit that referenced this issue Apr 24, 2024
Make `SelectionModel.SelectedItems` and `SelectionModel.SelectedIndexes` implement `INotifyCollectionChanged` so that they can be bound to.

As well as implementing `INotifyCollectionChanged` on the collections, we also had to implement `IList` (see #8764) so refactored this out into a base class.

For the sake of simplicity, these collections only raise `Reset` for any change: this is may need to be changed later but I'd rather follow the KISS principle for the moment until something more complex is proven necessary.

Fixes #15497
maxkatz6 pushed a commit that referenced this issue Apr 25, 2024
Make `SelectionModel.SelectedItems` and `SelectionModel.SelectedIndexes` implement `INotifyCollectionChanged` so that they can be bound to.

As well as implementing `INotifyCollectionChanged` on the collections, we also had to implement `IList` (see #8764) so refactored this out into a base class.

For the sake of simplicity, these collections only raise `Reset` for any change: this is may need to be changed later but I'd rather follow the KISS principle for the moment until something more complex is proven necessary.

Fixes #15497
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug customer-priority Issue reported by a customer with a support agreement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant