简体   繁体   中英

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.

Two changes needed:

  1. The type of the bound property ( SelectedElement ) must match the type of the CollectionView property ( SelectedItems ). XF Doc says the type is IList<object> .

  2. Your bound property (if the correct type) will get set to the SelectedItems list when the binding is resolved (page is loaded; BindingContext set). It starts out as an empty list. As selection changes, the list's contents change. But the list object itself is the same object, so the setter never gets called again.

So how do you see a change? Via either SelectionChangedCommand , or SelectionChanged event .


To use SelectionChangedCommand :

add to CollectionView XAML:

<CollectionView ... SelectionChangedCommand="{Binding SelectionChangedCommand}" ...>

Add to view model (your BindingContext):

    // There's no point in customizing this setter, because it won't get called when you want it to.
    public IList<object> SelectedElement { get; set; }

    // in constructor:
         SelectionChangedCommand = new Command(SelectionChanged);

    public Command SelectionChangedCommand { get; set; }

    private void SelectionChanged(object obj)
    {
        if (SelectedElement != null && SelectedElement.Count > 0) {
            var element = SelectedElement[0] as Element;
            ...
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM