command for ContextMenu in DataTemplate

小熊為你居然被註冊了發表於2013-11-13

準備教程。

 

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}" />

           

 

相關文章