Forum Xamarin.Forms

How can i get selected item with button click event in listview?

    <ListView ItemsSource="{Binding .}"
              HasUnevenRows="True"
              IsPullToRefreshEnabled="True"
              Refreshing="RefreshItems"
              BackgroundColor="White"
              x:Name="listtodo"
              ItemSelected="SelectedItem">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.2*"/>
                            <ColumnDefinition Width="0.6*"/>
                            <ColumnDefinition Width="0.2*"/>
                        </Grid.ColumnDefinitions>
                            <Image Source="{Binding ProductImageUrl}"
                               VerticalOptions="Center"
                               HorizontalOptions="Start"
                               x:Name="txtProductImage"
                               WidthRequest="100"
                               Aspect="AspectFit"
                               HeightRequest="100"
                               Margin="10,0,0,0"
                               Grid.Column="0"/>
                            <Label Text="{Binding ProductName}"
                               x:Name="txtProductName"
                               FontSize="Medium"
                               TextColor="Black"
                               VerticalOptions="Center"
                               HorizontalOptions="Start"
                               Grid.Column="1"/>
                            <ImageButton Source="checked.png"
                                         VerticalOptions="Center"
                                         x:Name="btnchecked"
                                         HorizontalOptions="Center"
                                         Command="{Binding ButtonClicked}"
                                         CommandParameter="{Binding}"
                                         Grid.Column="2"
                                         Aspect="AspectFit"
                                         BackgroundColor="#FF5D7F"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

This is my xaml code how can i get selected item from c#

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I have seen you have set the ImageButton's CommandParameter if you want to get the current item when ImageButton clicked, you could get that in the Command:

    ButtonClicked = new Command((object item) =>
    {
        // Get the selected item here
        Model model = item as Model;
    });
    

    If you want to get the selected item in the list view's SelectedItem event, you can obtain it directly:

    private void SelectedItem(object sender, SelectedItemChangedEventArgs e)
    {
        Model selectedItem = e.SelectedItem as Model;
    }
    
  • _Jai_Jai USMember ✭✭

    @EbubekirSezer have you solved this . I also want the selected item when I am clicking on image inside the list view . can you please help in that. I did this solution with command parameters its not working with me. can you share the code with me if it works for you. thank you.

Sign In or Register to comment.