Laziale
Laziale

Reputation: 8225

Get Model item in Xamarin on button clicked

I have this listview in Xamarin:

            <ListView x:Name="LocationsListView"
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Margin="10,10,10,10" Padding="5,5,5,5">
                                <StackLayout Grid.Column="0">
                                    <Label x:Name="lblAirport" HorizontalOptions="Center" Text="{Binding AirportICAO}"  />
                                    <Button x:Name="btnConfirmFuel" IsVisible="{Binding AirportFuelSelected}" Clicked="BtnConfirmFuel_Clicked" CommandParameter="{Binding .}" />
                                </StackLayout>
                            </Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

I want to get the item in the list which relates to the row where the button is clicked. Now I'm able to see the things I want to get through Clicked object sender parameter:

enter image description here

But when I'm trying to get the actual CommandParameter I don't get that as an option. What are my options, how can I detect from which item the button is clicked in the Xamarin Listview?

Upvotes: 0

Views: 350

Answers (1)

Jason
Jason

Reputation: 89102

just cast the sender and get its CommandParameter

var item = (Model)((Button)sender).CommandParameter;

Upvotes: 2

Related Questions