CollectionView SelectedItems Two Way Binding doesn't work
Description
If you bind SelectedItems with Mode=TwoWay, selecting an item should add it to the list. But it doesn't.
The documentation says "The default binding is one-way", implying that two-way should be possible.
Steps to Reproduce
<CollectionView x:Name="collectionView"
IsGrouped="True"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedItems, Mode=TwoWay}">
...
</CollectionView>
public partial class MyContentView : ContentView
{
public ObservableCollection<char> SelectedItems => new ObservableCollection<char>();
public MyContentView()
{
InitializeComponent();
BindingContext = this;
}
}
Link to public reproduction project repository
No response
Version with bug
8.0.61 SR6.1
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
all
Did you find any workaround?
Hook SelectionChanged, manually determine difference between e.PreviousSelection and e.CurrentSelection, then manually add/remove items from SelectedItems
foreach (var addedItem in e.CurrentSelection.Except(e.PreviousSelection).Cast<KanjiSelectionListNewModelKanji>())
{
SelectedKanji.Add(addedItem.Character);
}
foreach (var removedItem in e.PreviousSelection.Except(e.CurrentSelection).Cast<KanjiSelectionListNewModelKanji>())
{
SelectedKanji.Remove(removedItem.Character);
}
Relevant log output
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Open similar issues:
- CollectionView is not updating SelectedItems bound property (#8435), similarity score: 0.82
- Cannot preselect items in CollectionView when it's multiselect on Windows, iOS and macOS (#15718), similarity score: 0.78
- .NET MAUI - CollectionView SelectedItem binding functions on Windows but not Android platform (#9523), similarity score: 0.76
- Multiselection for Collectionview doesn't works on MacCatalyst (#18028), similarity score: 0.75
Closed similar issues:
- CollectionView's selected item and another control bound to the same property behave incorrectly (#16582), similarity score: 0.78
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
I don't think this is the same as #8435 because changing it to ObservableCollection<object> does not work around the issue.
Change "ObservableCollection<char>" to "IList<object>" works for me
ok with the above hint, I got this working:
- The collection needs to be
ObservableCollection<object> - It needs to be a property, not a field
- It needs to be on the ViewModel, not the ContentView
- With grouping enabled, the types of the items will be the enumerated-type of the group, even if you use an
ItemTemplate. (In retrospect this should've been obvious, but I guess I had a brain fart. Support for proper typing on the collection would've caught this...) - For some reason (???) the ObservableCollection seems to be cloning the selected items, meaning you can't use reference-equality when comparing them
If you screw any of these up, the binding will just silently fail, with no error message. Interestingly, I just learned about the "XAML Bindings Failure" window, but even with the above changes (and working code), the window claims the binding failed 🤦
So I guess this bug can be closed