command for ContextMenu in DataTemplate
准备教程。
1. 无难度方式
<DataTemplate x:Key="DataTemplate1">
<StackPanel >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu >
<toolkit:MenuItem Tag="{Binding}" Click="MenuItem_Click" />
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem item = sender as MenuItem;
A rd = item.Tag as A;
}
2.command摸索中 此时拿到的事整体的数据源
<DataTemplate x:Key="DataTemplate1">
<StackPanel >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu >
<toolkit:MenuItem Command="{Binding MyCommand}" CommandParameter="{Binding}" >
<toolkit:MenuItem.DataContext>
<listdata:ViewModel/>
</toolkit:MenuItem.DataContext
3.此时效果与1相同
<DataTemplate x:Key="DataTemplate1">
<StackPanel >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu >
<toolkit:MenuItem Command="{Binding DataContext.MyCommand, ElementName=LayoutRoot}" />