maui icon indicating copy to clipboard operation
maui copied to clipboard

CollectionView SelectedItems Two Way Binding doesn't work

Open BlueRaja opened this issue 1 year ago • 2 comments

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

BlueRaja avatar Jun 29 '24 00:06 BlueRaja

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.

github-actions[bot] avatar Jun 29 '24 00:06 github-actions[bot]

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

BlueRaja avatar Jun 29 '24 03:06 BlueRaja

Change "ObservableCollection<char>" to "IList<object>" works for me

davidce avatar Dec 24 '24 08:12 davidce

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

BlueRaja avatar Feb 19 '25 12:02 BlueRaja