我的ContextMenu有点问题,有些时候在单击ContextMenu时没有在LongList中选择项目,这给我一个问题,我不知道用户点击了什么项目。如果我先点击该项目,然后按住上下文菜单,它会起作用,但是每次用户点击并保持时,我怎样才能使这个工作?
这是我的xaml
<phone:LongListSelector x:Name="List"
Margin="0,0,-12,0"
ItemsSource="{Binding ListItems}"
Height="470" Tap="List_OnTap">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Text="{Binding Name}"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu IsZoomEnabled="false">
<toolkit:MenuItem Header="Add as favorit"
Click="AddFavorite" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</TextBlock>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
我的代码背后
private void AddFavorite(object sender, RoutedEventArgs e)
{
ItemViewModel obj = List.SelectedItem as ItemViewModel;
if (obj == null) return;
NavigationService.Navigate(new Uri("/Page.xaml?id=" + obj.Id + "&fav=true", UriKind.Relative));
}
答案 0 :(得分:1)
您可以从ContextMenu本身获取该项目。菜单将DataContext设置为绑定的元素。只需获取DataContext就可以了!
private void AddFavorite(object sender, RoutedEventArgs e)
{
var element = (FrameworkElement)sender;
ItemViewModel obj = element.DataContext as ItemViewModel;
if (obj == null) return;
NavigationService.Navigate(new Uri("/Page.xaml?id=" + obj.Id + "&fav=true", UriKind.Relative));
}