How to get selected element of list in BindableLayout with TapGestureRecognizer

fsulserfsulser Member ✭✭
edited March 2019 in Xamarin.Forms

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.

Answers

Sign In or Register to comment.