0
votes

When a Add to cart button is clicked i want clicked row to add to local database.

i tried to implement clicked option, but is shows System.InvalidCastException: Specified cast is not valid.

My Xaml is :

 <controls:FlowListView x:Name="gallery"
                           FlowColumnCount="2"
                           SeparatorVisibility="Default"
                           HasUnevenRows="True"
                           FlowItemsSource="{Binding ItemsGallery}"
                           FlowUseAbsoluteLayoutInternally="True"
                           FlowItemTapped="OnFlowItemTapped"
                           FlowColumnExpand="Proportional"

                           BackgroundColor="White">
        <controls:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame BorderColor="#DCDCDC" HasShadow="True" Margin="2,2,2,2" CornerRadius="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="100"/>
                                <RowDefinition Height="35"/>
                                <RowDefinition Height="20"/>
                                <RowDefinition Height="20"/>
                                <RowDefinition Height="35"/>
                            </Grid.RowDefinitions>
                            <ffimageloading:CachedImage Source="{Binding image}" Grid.Row="0" LoadingPlaceholder="ItemsGallery" HeightRequest="120" WidthRequest="120" />
                        <Label Grid.Row="1"
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           HorizontalTextAlignment="Start"

                               TextColor="Black"
                           Text="{Binding name}" />

                            <Label Grid.Row="2" HorizontalOptions="Start"
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           Text="{Binding weight}" />

                            <Label HorizontalOptions="Start" Grid.Row="3" 
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           Margin="2"
                           Text="{Binding price}" />
                            <Button Text="Add TO Cart"  Grid.Row="4" BackgroundColor="Coral" TextColor="WhiteSmoke" Clicked="Button_Clicked_1" />
                        </Grid>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </controls:FlowListView.FlowColumnTemplate>
    </controls:FlowListView>

and my logic is :

private async void Button_Clicked_1(object sender, EventArgs e) {

    if (((ListView)sender).SelectedItem == null)
        return;

    var myList = (ListView)sender;
    var myProuct= (myList.SelectedItem as ProductDetail);


}
2
The sender is a button, what are the elements in the ItemsGallery?ProductDetail? Can you share the code behind? Like the model and code of ItemsGallery?Jack Hua

2 Answers

0
votes

the sender of a Button Click event will be a BUTTON, not the ListView containing the button

private async void Button_Clicked_1(object sender, EventArgs e) {

    // the sender of a button click event is a BUTTON
    var btn = (Button)sender;

    var myProduct = (ProductDetail)btn.BindingContext;
}
0
votes

You could go with regular data binding way with viewmodel, or you can try this

Button Text="Add TO Cart"  Grid.Row="4" BackgroundColor="Coral" TextColor="WhiteSmoke" Command="{Binding AddCommand}" CommandParameter = "{Binding .}" 

But then you would need to edit the model building up ItemsGallery list.

public ICommand AddCommand { get; set; }

And when you are creating your objects

new Gallery
{
           //your stuff
           AddCommand = new Command<Gallery>(async (item) => await 
           AddItem(item))
})



 private async Task AddItem(Gallery item)
 {
        //your implementation
 }