1.在工程references中引入Microsoft.Phone.Controls.Toolkit。
2.在需要实现的页面(例如,demo.xaml)文件中加入toolkit声明如下:
- xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
- <!--ContentPanel - place additional content here-->
- <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
- <Grid Background="Blue">
- <TextBlock Text="Select a color from the Context Menu"/>
- <ListBox x:Name="listBox">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <toolkit:ContextMenuService.ContextMenu>
- <toolkit:ContextMenu>
- <toolkit:MenuItem Header="Add Color" Click="MenuItem_Click"/>
- <toolkit:MenuItem Header="Remove Color" Click="MenuItem_Click"/>
- </toolkit:ContextMenu>
- </toolkit:ContextMenuService.ContextMenu>
- <Image Source="{Binding ImageUri}" Stretch="None" />
- <TextBlock Text="{Binding Text}" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
- </StackPanel>
最重要的想要获取MenuItem 点击后 Click要获取的当前的ListBoxItem
private void delOne_Click(object sender, System.Windows.RoutedEventArgs e)
{
ListBoxItem item = scheduleItemList.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext) as ListBoxItem;
//下面在可以操作该item了
}