I have a GridView
like below
You should set SelectionMode="Single"
in gridview and set IsRightTapEnabled="True"
.
My model has a class Student
:
public class Student : NotifyProperty
{
public string Name
{
set
{
_name = value;
OnPropertyChanged();
}
get
{
return _name;
}
}
private string _name;
}
And my viewmodel has a List<Student>
.
I set the list as gridview's source.
My gridview is
<GridView x:Name="SymbolGridView"
SelectionMode="Single"
IsItemClickEnabled="True"
IsRightTapEnabled="True"
ItemsSource="{x:Bind View.Student}"
ItemClick="SymbolGridView_OnItemClick"
RightTapped="SymbolGridView_OnRightTapped">
<GridView.ItemTemplate>
<DataTemplate x:DataType="view:ViewModel">
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
The SymbolGridView_OnRightTapped
have a OriginalSource that is TextBlock.But if your DataTemlpate is a Grid ,the OriginalSource is Grid.
We can use var student = (e.OriginalSource as TextBlock)?.DataContext as Student;
to get the student.
The OriginalSource.DataContext is your selects item
But if you use the Grid,the OriginalSource may is ListViewItemPresenter.So the easy methor is use var student = (e.OriginalSource as FrameworkElement)?.DataContext as Student;
See: http://lindexi.oschina.io/lindexi/post/win10-uwp-%E5%8F%B3%E5%87%BB%E9%80%89%E6%8B%A9GridViewItem
If the SymbolControl in your ItemTemplate is a bit more complex and the elements in it may have their own DataContexts you could get a reference to the parent ListViewItemPresenter using the following helper method and then cast the DataContext of this to your Symbol item:
private void SymbolGridView_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
ListViewItemPresenter lvi = e.OriginalSource as ListViewItemPresenter;
if (lvi == null)
lvi = FindParent<ListViewItemPresenter>(e.OriginalSource as DependencyObject);
if (lvi != null)
{
SymbolItem clickedItem = lvi.DataContext as SymbolItem;
if (clickedItem != null)
MyMediaElement.Source = new Uri(this.BaseUri, symbolItem.ExampleAudio);
}
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}