TapGesture on Image within ViewCell doesn't update SelectedItem

CasperNybroeCasperNybroe USMember
edited June 2016 in Xamarin.Forms

I have created a ListView where each cell has its own image. When clicking the image, my command is fired and the method "OpenImageAsync" in the ViewModel is reached but the SelectedItem is not updated. It is always NULL.
I have tried to follow this guide but with no luck.
http://rasmustc.com/blog/Image-Gallery-With-Xamarin-Forms/

Xaml
<ListView x:Name="ListViewName" ItemsSource="{Binding PollStatistics}" SelectedItem="{Binding SelectedPollStatistic}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <Image Source="{Binding Image, Converter={StaticResource ByteArrayToImage}}"> <Image.GestureRecognizers> <TapGestureRecognizer Command="{Binding Path=BindingContext.OpenImageCommand, Source={x:Reference EndPageContentPage}}" CommandParameter="Image" /> </Image.GestureRecognizers> </Image> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

Xaml.cs
public partial class EndPage : BaseContentPage { public EndPage(INavigation navigation, User user) { InitializeComponent(); this.BindingContext = new EndPageViewModel(navigation, user); } }

ViewModel
public class EndPageViewModel : BaseViewModel, IIsLoadSpinnerRunning { private readonly INavigation _navigation; private readonly User _user; private readonly PollStatisticManager _pollStatisticManager = PollStatisticManager.DefaultManager; private ObservableCollection<PollStatisticDTO> _pollStatistics; private PollStatisticDTO _selectedPollStatistic; public EndPageViewModel(INavigation navigation, User user) { this._navigation = navigation; this._user = user; OpenImageCommand = new Command(async () => await OpenImageAsync()); PollStatistics = await _pollStatisticManager.GetPollStatisticsForSessionAsync(sessionData); } public ObservableCollection<PollStatisticDTO> PollStatistics { get { return _pollStatistics; } set { _pollStatistics = value; OnPropertyChanged("PollStatistics"); } } public PollStatisticDTO SelectedPollStatistic { get { return _selectedPollStatistic; } set { _selectedPollStatistic = value; OnPropertyChanged("SelectedPollStatistic"); } } public ICommand OpenImageCommand { get; set; } private async Task OpenImageAsync() { if (SelectedPollStatistic != null) { await _navigation.PushModalAsync(new ImageModalPage(_navigation, SelectedPollStatistic.Image)); } } }

Sign In or Register to comment.