문제

have a little problem with my ContextMenu, some times the item is not selected in the LongList when the ContextMenu is clicked, this gives me the problem that i do not know what item the user have clicked. If i cickly click the item first and then hold for the Context Menu dose it work, but how can i make this work every time the user click and holds?

Here is my 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>

And my code behind

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));
}
도움이 되었습니까?

해결책

You can get the item from the ContextMenu itself. The menu has it's DataContext set to the element that is being bound to. Simply get the DataContext and you're off!

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));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top