Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to get selected element of list in BindableLayout with TapGestureRecognizer

$
0
0

I have a page with multiple images in it. As the example is only a part of it I don't use a ListView, but instead a StackLayout with a BindableLayout.
To recognize taps on the rows I added a TapGestureRecognizer. How can I get which row/element from my list is selected?

In my xaml I created:

        <StackLayout BindableLayout.ItemsSource="{Binding ImageList}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Vertical">
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding RowTappedCommand}"/>
                        </StackLayout.GestureRecognizers>

                        <Image Source="{Binding ImageURL}"/>
                        <Label Text="{Binding LabelText}"/>
                    </StackLayout>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>

In my ViewModel I have

    public class ImageListViewModel
    {
        public ObservableCollection<Image> ImageList { get; set; }
        public ICommand RowTappedCommand { get; private set; }

        public ImageListViewModel()
        {
            News = new ObservableCollection<News>();
            RowTappedCommand = new Command(RowTapped);
        }

        void RowTapped()
        {
            //get selected value;
        }
     }

I tried to use CommandParameter ="{Binding .}" but this will have the complete object and no indication on the row.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>