dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.18k stars 1.74k forks source link

CollectionView SelectedItems Two Way Binding doesn't work #23358

Open BlueRaja opened 4 months ago

BlueRaja commented 4 months ago

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

github-actions[bot] commented 4 months ago

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:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

BlueRaja commented 4 months ago

I don't think this is the same as #8435 because changing it to ObservableCollection<object> does not work around the issue.