I’m building a Windows 8 Metro application (C#, XAML) with a GridView, and I’d like to show a popup near the GridViewItem that the user clicked on. I was planning on getting the GridViewItem UIElement through the click event arguments and using its coordinates to determine where to place my popup.
However, I haven’t yet found a way to get a reference to the actual GridViewItem UIElement that was clicked! The events that I’ve tried below seem to expose only the DataContext through their EventArgs, and not the actual UIElement:
object sender // is the GridView
ItemClickEventArgs.ClickedItem // is the DataContext of the GridViewItem
ItemClickEventArgs.OriginalSource // is the GridView
SelectionChangedEventArgs.OriginalSource // is null
SelectionChangedEventArgs.AddedItems.First() // is the DataContext of the GridViewItem
If it matters, my GridView.ItemSource is a CollectionViewSource, with its Source bound to a collection of viewmodels.
Is it possible to get the clicked GridViewItem through some event I’ve overlooked? If not, what angle should I look into to work around it? I can at least get the click coordinates relative to the GridView through the PointerPressed event and see what I can do to locate the item that way, but really hoping I don’t have to go down that route.
VisualTreeHelper might help you placing it to either the GridView/GridViewItem and finding it’s child/parent that is if you want to find the UIElement. To determine what UIElement was click inside a container should be solve using the VisualTreeHelper.
More on VisualTreeHelper is found here Visual Tree Helper example
IF you don’t want to use VisualTreeHelper, I would create an attach property that all my GridViewItem click will be pass themselves as dependencyObject to the the GridView’s attach property (Which is custom and you can get more out of it).