I have a GridView which display an image in a Grid. This App is based on PhotoLab Sample.
ImageGridView_ContainerContentChanging is called when GridWiew is populated.
When it's done, I would like to use RightTapped on the grid (which contains an image) to do an action that needs to know the item
I can get the Item as discribed on Grid_RightTapped event
if I disable ImageGridView_ContainerContentChanging, but of course the image is not displayed.
The issue is when I use ContainerContentChanging the DataContext becomes null !
private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Item item = ((FrameworkElement)e.OriginalSource).DataContext as Item;
Debug.WriteLine($"ItemDescription: {item.ItemDescription}");
mListView.SelectedItem = item;
}
private void ImageGridView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.InRecycleQueue)
{
Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;
Image image = (Image)templateRoot.FindName("VignetteImage");
image.Source = null;
}
if (args.Phase == 0)
{
args.RegisterUpdateCallback(ShowImage);
args.Handled = true;
}
}
private async void ShowImage(ListViewBase sender, ContainerContentChangingEventArgs args)
{
vGlobale.isLoadingPicture = true;
if (args.Phase == 1)
{
Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;
Image image = (Image)templateRoot.FindName("VignetteImage");
image.Opacity = 100;
ImageFileInfo item = args.Item as ImageFileInfo;
try
{
BitmapImage itemTemp = await item.GetImageSourceAsync(540);
Debug.WriteLine($"PixelWidth {itemTemp.PixelWidth}");
if (itemTemp.PixelWidth == 0)
{
image.Source = await item.GetImageThumbnailAsync();
}
else
{
image.Source = itemTemp;
}
}
catch (Exception )
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri(image.BaseUri, "/Assets/ImageInconnue.png");
image.Source = bitmapImage;
}
}
vGlobale.isLoadingPicture = false;
}
What I have tried:
Everything works separately but both together not.
Need help to find a solution to get the Item in RightTapped event (perhaps without DataContext)