Click here to Skip to main content
15,791,356 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.Handled = true;

private async void ShowImage(ListViewBase sender, ContainerContentChangingEventArgs args)
    vGlobale.isLoadingPicture = true;
    if (args.Phase == 1)
        // It's phase 1, so show this item's image.
        Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;

        Image image = (Image)templateRoot.FindName("VignetteImage");
        image.Opacity = 100;
        ImageFileInfo item = args.Item as ImageFileInfo;
        //Debug.WriteLine($"item : {item.ImageName}");

            BitmapImage itemTemp = await item.GetImageSourceAsync(540);
            Debug.WriteLine($"PixelWidth {itemTemp.PixelWidth}");

            if (itemTemp.PixelWidth == 0)
                image.Source = await item.GetImageThumbnailAsync();
                image.Source = itemTemp;
        //catch (IOException e)
        catch (Exception )
            //Debug.WriteLine($"exeption {e}");
            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)
Updated 30-Jun-21 9:13am
Richard Deeming 29-Jun-21 11:26am    
NB: Using x as Type will return null if the value is not of the specified type. You will need to check for null before trying to access properties or methods on the returned value.
Philosopheur 29-Jun-21 11:39am    
Thant for your answer,
Yes I can check if DataContext is null before but my question is What should I do to get the "new" DataContext or how I can "Update" DataContext ?
Gerry Schmitz 30-Jun-21 13:31pm    
You're describing actions and events with no reference to controls; we're suppossed to infer that you're using grids, and listviews and what not; who's displaying what; and how they're related. Well, it's not possible.
Philosopheur 30-Jun-21 14:19pm    
I have a GridView and its DataTemplate contains a grid with several controls including an Image control
Gerry Schmitz 30-Jun-21 19:31pm    
That was part 1 of at least 3 parts. Stick to your short descriptions and expect useless answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900