RedZ
RedZ

Reputation: 1013

Xamarin - CollectionView, pass current item as a command parameter

I Have a CollectionView which is bound to an ObservableCollection, inside each row of the view i have a (LIKE/UNLIKE) button, when that button is clicked, a command on the ViewModel is called, which should read the ID + state (button checked/unchecked) to determine if it should execute a LIKE or UNLIKE API call. My question is, how do i pass the current item (parent row of the button) as a command parameter (of type Product) ?

Here is my XAML:

<CollectionView ItemsSource="{Binding Products}" SelectionMode="Single" SelectedItem="{Binding SelectedProduct , Mode=TwoWay}" RemainingItemsThreshold="{Binding ItemsThreshold}" RemainingItemsThresholdReachedCommand="{Binding LoadProductsCommand}" BackgroundColor="Fuchsia">
                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical" VerticalItemSpacing="10"></GridItemsLayout>
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Frame BorderColor="Black" BackgroundColor="Green" HeightRequest="100" Padding="0" Margin="0 , 10" HasShadow="True">
                            <Grid Padding="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"></RowDefinition>
                                    <RowDefinition Height="*"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>

                                <Image Source="{Binding photo_1}" Aspect="AspectFill" Grid.Row="0" Grid.Column="0"></Image>

                                <StackLayout Grid.Row="1" Grid.Column="1" BackgroundColor="Yellow" HorizontalOptions="End">
                                    <button:SfButton Text="HEART" IsCheckable="True" IsChecked="{Binding liked}" Command="{Binding Source={x:Reference MyProductsPage} , Path=BindingContext.LikeCommand}" CommandParameter="{Binding ???}"></button:SfButton>
                                    <Label Text="{Binding total_likes}" HorizontalTextAlignment="Center"></Label>
                                </StackLayout>

                            </Grid>
                        </Frame>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

Upvotes: 6

Views: 3679

Answers (1)

nevermore
nevermore

Reputation: 15816

Just binding like this:

<button:SfButton Text="HEART" IsCheckable="True" IsChecked="{Binding liked}" Command="{Binding Source={x:Reference MyProductsPage} , Path=BindingContext.LikeCommand}" CommandParameter="{Binding .}"></button:SfButton>

Here is the code example: ContactView

Upvotes: 7

Related Questions