Search code examples
c#xamarin.formsmvvm-light

CollectionView, SelectedItems="{Binding SelectedElement, Mode=TwoWay}" Didn't work


I am trying to bind the "SelectedItems" property of my CollectionView between my View and my ViewModel. But the TwoWay didnt work.

I can send information to my View, but i cant send information to my ViewModel.

( all another biding works correctly )

Some code,in my ViewModel :

        private ObservableCollection<Element> _selectedElement;
        public ObservableCollection<Element> SelectedElement
        {
            get
            {
                return _selectedElement;
            }
            set
            {
                if (_selectedElement != value)
                {
                    _selectedElement = value;

                }
            }
        }

in my view

<CollectionView  ItemsSource="{Binding Elements,Mode=TwoWay}" 
                             SelectionMode="Multiple"
                             SelectedItems="{Binding SelectedElement, Mode=TwoWay}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout  Orientation="Horizontal" >
                            
                            <StackLayout   Padding="15,10" Orientation="Horizontal" >
                                <Label Text="{Binding Libelle}" VerticalOptions="Center" />
                                <Label Text="{Binding Code}" VerticalOptions="Center" />
                            </StackLayout>
                            
                            <StackLayout HorizontalOptions="EndAndExpand" VerticalOptions="Center" >
                                <ImageButton Source="{Binding ImgFavori}"
                                                 BackgroundColor="Transparent"
                                                 Command="{Binding Path=BindingContext.ManageFavCommand, Source={x:Reference CurrentView}}"
                                                 CommandParameter="{Binding .}"
                                                 WidthRequest="75" 
                                                VerticalOptions="Fill"/>
                            </StackLayout>
                            
                            </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

When i use the ViewModel for manipulate the view like this :

private async void ClearCollection()
{
    SelectedElement = null;
}

that's work.

But when i try to select some line on my smartphone, i never pass in the setter of SelectedElement.

I am using Xamarin.Forms 5.0.0.2196

Thx for help me.


Solution

  • With your help I found my problem.

    I dont need to use the SelectionChangedCommand ( he work but i dont need this event, i just need data )

    But trying your answer, i added this :

    SelectionChangedCommandParameter="{Binding .}
    

    And now my IList SelectedElement has the data selected in my view !

    Thx for help me.