Mahajan344
Mahajan344

Reputation: 2550

Image inside Grid of listview click or tap event

I am working on xamarin forms cross platform app. I have listview inside that there is Grid and inside Grid there is image. I want to fire event with selected item/image but I don't know how I can do that.

I have found that this can be achieved using GestureRecognizers but how do I implement in my viewmodel

Please share some example with complete code, I mean xaml as well as code behind

Upvotes: 1

Views: 966

Answers (1)

Ziyad Godil
Ziyad Godil

Reputation: 2680

Try below code :

<ListView x:Name="InvoiceItemList" HasUnevenRows="true" ItemsSource="{Binding InvoiceLineItems}" SeparatorVisibility="None">
 <ListView.ItemTemplate>
    <DataTemplate>
       <ViewCell>
          <Image Source="overlaybg" Aspect="Fill">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="{Binding Source={x:Reference InvoiceItemList}, Path=BindingContext.RemoveInvoiceItemCommand}" />
                </Image.GestureRecognizers>
            </Image>
       </ViewCell>
    </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

EDIT

ViewModel Code

declare command

public ICommand RemoveInvoiceItemCommand { get; }

Initalize command in constructor

RemoveInvoiceItemCommand = new Command(() =>
            {
               RemoveInvoiceItem();
            });

Create function

 public void RemoveInvoiceItem()
    {
       //Do stuff
    }

Upvotes: 7

Related Questions