WP8工具箱上下文菜单项未在挂起时选择



我的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));
}

您可以从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));
}

相关内容

  • 没有找到相关文章

最新更新