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

EbubekirSezerEbubekirSezer ✭✭Member ✭✭
    <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#

Answers

  • LandLuLandLu Xamurai 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;
    }
    
Sign In or Register to comment.