Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

CollectionView SelectionMode SelectionMode="Multiple" Not working #5625

Closed
psp589 opened this issue Mar 20, 2019 · 17 comments
Closed

CollectionView SelectionMode SelectionMode="Multiple" Not working #5625

psp589 opened this issue Mar 20, 2019 · 17 comments

Comments

@psp589
Copy link

psp589 commented Mar 20, 2019

Description

I can't get to work the selection multiple in CollectionView, I can't select more than one item, also I didn't see a SelectedItems property, only SelectedItem

Steps to Reproduce

Having this XAML

<ContentPage>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <CollectionView Grid.Column="0" 
                            SelectionMode="Multiple"
                            SelectedItems="{Binding SelectedItems, Mode=TwoWay}"
                            ItemsSource="{Binding ObservableCollectionTest}">
                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical" />
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Label Text="{Binding}"></Label>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
                
            </CollectionView>
            <StackLayout Orientation="Vertical" Grid.Column="1">
                  <Label Text="SelectedItems" />
                   <ListView ItemsSource="{Binding SelectedItems}"
                          WidthRequest="400"
                          BackgroundColor="Green"
                          HorizontalOptions="Center">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <Label Text="{Binding}"></Label>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>    
                </ListView>
            </StackLayout>
        </Grid>
    </ContentPage>

With this data in the Model

public static string SelectedItemsPropertyName() { return nameof(SelectedItems); }
        private ObservableCollection<string>_SelectedItems;
        public ObservableCollection<string>SelectedItems
        {
            get
            {
                return _SelectedItems;
            }
            set
            {
                _SelectedItems = value;
                RaisePropertyChanged();
            }
        }
        public static string ObservableCollectionTestPropertyName() { return nameof(ObservableCollectionTest); }
        private ObservableCollection<string> _ObservableCollectionTest = new ObservableCollection<string>(new List<string>() { "Test1", "test2", "test3" } );
        public ObservableCollection<string> ObservableCollectionTest
        {
            get
            {
                return _ObservableCollectionTest;
            }
            set
            {
                _ObservableCollectionTest = value;
                RaisePropertyChanged();
            }
        }
SelectedItem = ObservableCollectionTest[1];

Expected Behavior

Could select more than 1 item

Actual Behavior

is not possible

Basic Information

  • Version with issue: 4.0.0.232914-pre6 ( not working neither in 4.0.0-pre8)
  • IDE: Visual Studio Community 2017 version 15.9.9
  • Platform Target Frameworks:
    • Android: 8.1
    • UWP: Windows 10 version 1803 Build 17134
@adrianknight89
Copy link
Contributor

#5352

@psp589
Copy link
Author

psp589 commented Mar 20, 2019

#5352 says Android and IOS, what with UWP ? @adrianknight89

@adrianknight89
Copy link
Contributor

Not sure if @hartez is still updating https://gist.github.com/hartez/7d0edd4182dbc7de65cebc6c67f72e14, but I think the change was implemented only for Android and iOS for now.

@samhouts
Copy link
Member

This should be in 4.0.0-pre8. Thanks!

@psp589
Copy link
Author

psp589 commented Apr 4, 2019

@samhouts I've just tried 4.0.0-pre8 and it is still not working,
in UWP I can't still select more than one item
in Android I can, but the two way binding is not working (it works in mode= "single" but not in "Multiple") so in the UI I can see more than 1 item selected but it doesnt get reflected in the viewmodel
Could this issue be re-opened?

@samhouts
Copy link
Member

samhouts commented Apr 8, 2019

@psp589 We're currently focusing on implementing all features for Android and iOS before moving on to UWP.

We'll take a look at Android and see what's missing. Thanks!

@psp589
Copy link
Author

psp589 commented Apr 9, 2019

@samhouts I'm working with both Android and UWP, should I add then different issues when reporting ? one for Android and one for UWP ? Should I add for this issue, a different one for UWP ?

@samhouts samhouts added the s/unverified New report that has yet to be verified label Apr 9, 2019
@samhouts
Copy link
Member

@psp589 No need to report separate issues. Things that are not implemented in UWP yet will be tracked on the main ticket. Thanks!

@samhouts
Copy link
Member

@psp589 To make this easier to reproduce, can you please attach a small project that demonstrates this issue? Thanks!

@samhouts samhouts added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Apr 12, 2019
@psp589
Copy link
Author

psp589 commented Apr 12, 2019

@samhouts see https://github.com/psp589/XamarinFormsIssuesReproduction, last section marked as "Issue #5625"

@samhouts samhouts removed s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Apr 13, 2019
@samhouts samhouts modified the milestones: 4.0.0, 4.1.0 Apr 16, 2019
@samhouts samhouts added e/4 🕓 4 and removed s/unverified New report that has yet to be verified labels Apr 19, 2019
@pikausp
Copy link

pikausp commented May 14, 2019

To expand on this issue

  • The PropertyChanged gets called
  • The binding source is not updated

What helped me was creating a simple bridge behavior that proxied SelectedItems by cloning value of the source SelectedItems property.

I believe the issue here is that the instance of the SelectionList remains the same for the lifetime of the view and so the bindings are not updated. Since there is a manual call to OnPropertyChanged

OnPropertyChanged(SelectedItemsProperty.PropertyName);

_selectableItemsView.SelectedItemsPropertyChanged(oldItems, Copy());

the PropertyChanged is indeed invoked, but that does not trigger the system to update bindings.

@humhei
Copy link
Contributor

humhei commented Jun 22, 2019

image

  • Set vm selection to List
  • Clear VM seletion and items 1 and 2

Nothing will happened

PR #6629 should fixed this issue!

@francipvb
Copy link

Hello,

Can someone take a look on this again? I'm stuck trying to get this working on android. I have the screen reader enabled.

Cheers,

@hartez
Copy link
Contributor

hartez commented Mar 10, 2020

@francipvb Since this was closed so long ago and since you're using a screen reader, I would suggest opening a new issue.

@cdavidyoung
Copy link

Well, this explains why two way binding is not working in Multiple (at least in Android that I am testing in). I guess I am going to have to implement my own Multiple using Single. Why is this issue closed if it hasn't been fixed?

@cdavidyoung
Copy link

So even though two way binding is not working in Multiple I have been able to get multiple selection to work in my MVVM app. The trick is to declare and instantiate the list that is bound to SelectedItems with a type of object and cast it later:

    ObservableCollection<object> selectedWaypoints;
    public ObservableCollection<object> SelectedWaypoints
    {
        get => selectedWaypoints;
        set
        { // note that this is never called
           _userDialogs.Toast($"SelectedWaypoints: {value?.Count}", System.TimeSpan.FromSeconds(1));
           selectedWaypoints = value;
           RaisePropertyChanged(() => SelectedWaypoints);
        }
    }

In constructor:

        SelectedWaypoints = new ObservableCollection<object>();

bound to SelectionChangedCommand - cast to target class messageItem:

    public MvxCommand ItemSelectedCommand => new MvxCommand((() =>
    {
       if (SelectedWaypoints.Count> 0)
          _userDialogs.Toast($"ItemSelectedCommand: {SelectedWaypoints?.Count} {((messageItem)SelectedWaypoints?[SelectedWaypoints.Count-1]).Title}", System.TimeSpan.FromSeconds(1));
       else
          _userDialogs.Toast($"ItemSelectedCommand: {SelectedWaypoints?.Count}", System.TimeSpan.FromSeconds(1));
    }));

@luisyoroslav
Copy link

So even though two way binding is not working in Multiple I have been able to get multiple selection to work in my MVVM app. The trick is to declare and instantiate the list that is bound to SelectedItems with a type of object and cast it later:

    ObservableCollection<object> selectedWaypoints;
    public ObservableCollection<object> SelectedWaypoints
    {
        get => selectedWaypoints;
        set
        { // note that this is never called
           _userDialogs.Toast($"SelectedWaypoints: {value?.Count}", System.TimeSpan.FromSeconds(1));
           selectedWaypoints = value;
           RaisePropertyChanged(() => SelectedWaypoints);
        }
    }

In constructor:

        SelectedWaypoints = new ObservableCollection<object>();

bound to SelectionChangedCommand - cast to target class messageItem:

    public MvxCommand ItemSelectedCommand => new MvxCommand((() =>
    {
       if (SelectedWaypoints.Count> 0)
          _userDialogs.Toast($"ItemSelectedCommand: {SelectedWaypoints?.Count} {((messageItem)SelectedWaypoints?[SelectedWaypoints.Count-1]).Title}", System.TimeSpan.FromSeconds(1));
       else
          _userDialogs.Toast($"ItemSelectedCommand: {SelectedWaypoints?.Count}", System.TimeSpan.FromSeconds(1));
    }));

Works like a charm, thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants