×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    ContextMenu's MenuItem DataContext returns old items

    I am using a ContextMenu within a LongListSelector so that I can delete some items in the list bounded to the LLS.

    I am following a recent guide here in order to implement the LLS (though I am not using the JumpList). The only thing I've changed is to have the base group class extend ObservableCollection instead of List.

    The issue I am having is that once I've implemented the ContextMenu and delete from there, I can delete from the same "location" in teh visible list twice and then it would crash. Debugging shows that after the second delete, the Datacontext of the MenuItem returns the previous item that was deleted. So when I search for it in the list, the index I get is -1. I can catch this but I don't know how to then find out what was really selected as the item.

    My XAML section for the contextMenu is as below:

    Code:
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid toolkit:TiltEffect.IsTiltEnabled="True">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu  x:Name="conmen" Loaded="ContextMenu_Loaded">
                          <toolkit:MenuItem Header="Delete" Click="DeleteItem_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
    
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="Auto"/>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="Auto"/>
                 </Grid.ColumnDefinitions>
    
                 <Border Grid.Column="0" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                     <TextBlock Text="{Binding Usr, StringFormat='x{0}'}" FontSize="32" HorizontalAlignment="Left" Width="48"/>
                 </Border>
    
                 <Border Grid.Column="1" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                     <TextBlock Text="{Binding Name}" FontSize="32" HorizontalAlignment="Left" />
                 </Border>
    
                 <Border Grid.Column="2" Background="{StaticResource PhoneInverseBackgroundBrush}" Padding="{StaticResource PhoneTouchTargetOverhang}">
                    <TextBlock Text="{Binding Type, StringFormat=\{0:C\}}" FontSize="32" HorizontalAlignment="Right" />
                 </Border>
            </Grid>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
    And this is the start of my delete_click function to remove the item:

    Code:
        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {            
            var menItem = (MenuItem)sender;
            editCartItem = (Model.Cartitem)menItem.DataContext;
    
            cartIndex = editCartItem.Id;
    
            deleteIndex = this.cartList.FindIndex(FindItem);
    After two deletes, the (Model.Cartitem)menItem.DataContext returns the previously deleted item.

    I have been searching for a while and have found similar cases for different frameworks and scenarios from some years before. I wanted to know if there was an updated method for doing this in WP8.

    I have seen suggestions in manually re-assigning the datacontext of the ContextMenu with a Loaded or Opened event, but the DataContext still relies on a specific item in the LLS. So I can't just point it's context to the LLS's.

    I've also seen that it's been pointed to as a bug with a patch here which seems exactly like my issue, but I had no idea on how to apply the patch or if it even pertained to my situation with WP8.

    I've also been ensuring that the LLS's selected item is cleared and have tried re-assigning it's itemSource after each operation to no avail.

    Any help or advice in the right direction would be great. I've seen some posts on here about this, but I believe I've already passed those points (such as simply getting the menuItem and using an ObservableCollection...).

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: ContextMenu's MenuItem DataContext returns old items

    Where does your FindItem come from?

    Can you post a simple complete page that reproduces the problem?

    I tried this one:

    Code:
    <phone:PhoneApplicationPage
        x:Class="PhoneApp1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True">
    
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
                <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>
    
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    
                <phone:LongListSelector x:Name="list" ItemsSource="{Binding}">
    
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <Grid toolkit:TiltEffect.IsTiltEnabled="True">
                                <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu x:Name="conmen">
                                        <toolkit:MenuItem Header="Delete" Click="DeleteItem_Click"/>
                                    </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>
    
                                <TextBlock Text="{Binding}" Style="{StaticResource PhoneTextLargeStyle}" />
                            </Grid>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                    
                </phone:LongListSelector>
                
            </Grid>
    
        </Grid>
    
    </phone:PhoneApplicationPage>
    Code:
    namespace PhoneApp1
    {
        using Microsoft.Phone.Controls;
        using System.Collections.ObjectModel;
        using System.Windows;
    
        public partial class MainPage : PhoneApplicationPage
        {
            private ObservableCollection<string> data = new ObservableCollection<string>
                {
                    "one",
                    "two",
                    "three",
                    "four",
                    "five",
                    "six",
                    "seven",
                    "eight",
                    "nine",
                    "ten"
                };
    
            public MainPage()
            {
                InitializeComponent();
    
                this.DataContext = this.data;
            }
    
            private void DeleteItem_Click(object sender, RoutedEventArgs e)
            {
                list.SelectedItem = null;
    
                var menuItem = (MenuItem)sender;
                var item = (string)menuItem.DataContext;
    
                this.data.Remove(item);
            }
        }
    }
    Last edited by paulo.morgado; 2013-01-27 at 12:41.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Re: ContextMenu's MenuItem DataContext returns old items


Similar Threads

  1. Opening a ContextMenu placed in another file
    By pavneetarora in forum [Archived] Qt Quick
    Replies: 5
    Last Post: 2012-02-20, 17:25
  2. CheckBox MenuItem
    By 3kworld in forum Symbian User Interface
    Replies: 4
    Last Post: 2009-10-14, 14:31
  3. How to add a small icon on every MenuItem
    By sudeepa2005 in forum Symbian
    Replies: 6
    Last Post: 2008-12-23, 12:20
  4. How to open a Dialog from a MenuItem?
    By jennie in forum Symbian User Interface
    Replies: 5
    Last Post: 2007-08-22, 08:30
  5. CLogViewEvent returns deleted items
    By sasa in forum Symbian
    Replies: 1
    Last Post: 2007-03-07, 04:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×