Yash
Yash

Reputation: 177

Xamarin Forms TableView selected item handler?

I need to trigger a command when user clicks on the table cell.

I looked at documentation on tablecell, it doesn't talk about any click event handlers.

This is how my XAML looks -

<ContentPage Title="Default">
    <TableView>        
        <TableView.Root>
            <TableSection>
                <ViewCell>
                    <StackLayout Padding="10,0,0,0" Orientation="Horizontal">
                    <Image Source="..." />
                    <Button Text="Home" Command="{Binding NavigateCommand}" CommandParameter="Home" />
                    </StackLayout>
                </ViewCell>
            </TableSection>
        </TableView.Root>
    </TableView>     
</ContentPage>

Right now, command is triggered only when clicked on button, but I want to trigger command when the cell is clicked.

I am new to xamarin, is custom viewcell rendering the only way to achieve this?

If so, any pointers there?

Thanks!

Upvotes: 3

Views: 4843

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

You can use the Tapped gesture recognizer within a ViewCell.

Xaml:

<TableView>
    <TableView.Root>
        <TableSection>
            <ViewCell Tapped="OnViewCellTapped">
               ~~~~~
            </ViewCell>
        </TableSection>
    </TableView.Root>
</TableView>

Code behind:

protected void OnViewCellTapped(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Tapped");
}

Upvotes: 4

Related Questions