The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Context Menu RRS feed

  • Question

  • Hello,

    I am trying to initiate a file delete with a context menu following up a hold Event for Windows Phone 8.

    private void listBox_NoteList_Hold(object sender, System.Windows.Input.GestureEventArgs e)
            {
               FrameworkElement element = (FrameworkElement)e.OriginalSource;
                Note item = (Note)element.DataContext;
    
                ListBox listBox = sender as ListBox;
                var selectedItem = listBox.SelectedItem;
    
                //Context Menu here...
    
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(item.NoteID + ".txt"))
                    {
                        store.DeleteFile(item.NoteID + ".txt");
                    }
                }
    
                listBox_NoteList.ItemsSource = CreateNoteList();
            }

    How do I design a C# context menu which just shows "delete" and if delete is clicked, the file deletion and list re-creation procedure executes?

    I have tried the ContextMenu from WindowsPhone Toolkit, but always get "Object reference not set to an instance of an object" exception.

    <ListBox x:Name="listBox_NoteList" ItemsSource="{Binding list}" Margin="0,0,0,60" BorderThickness="1" Tap="listBox_NoteList_Tap" Hold="listBox_NoteList_Hold">
                    <tk:ContextMenuService.ContextMenu>
                        <tk:ContextMenu>
                            <tk:MenuItem x:Name="ContextMenu" Header="delete" Click="ContextMenu_Click"/>
                        </tk:ContextMenu>
                    </tk:ContextMenuService.ContextMenu>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="sp" Orientation="Vertical" Margin="0,5,0,0">
                                
                                <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LastEditTime}" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    private void ContextMenu_Click(object sender, RoutedEventArgs e)
            {
                FrameworkElement element = (FrameworkElement)e.OriginalSource;
                Note item = (Note)element.DataContext;
    
                ListBox listBox = sender as ListBox;
                var selectedItem = listBox.SelectedItem;
    
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(item.NoteID + ".txt"))
                    {
                        store.DeleteFile(item.NoteID + ".txt");
                    }
                }
    
                listBox_NoteList.ItemsSource = CreateNoteList();
            }

    Does anyone know how to get this working, either the "Toolkit Context Menu" or "C# Context Menu" way?

    Thanks...

    Tuesday, April 29, 2014 3:55 PM

Answers

  • In the meantime, I got the "Toolkit Context Menu" way working:

    private void ContextMenu_Click(object sender, RoutedEventArgs e)
            {
                Note note = (Note)(sender as MenuItem).DataContext;
                
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(note.NoteID + ".txt"))
                    {
                        store.DeleteFile(note.NoteID + ".txt");
                    }
                }
    
                listBox_NoteList.ItemsSource = CreateNoteList();
            }

    • Marked as answer by Marcus Runge Thursday, May 1, 2014 11:33 AM
    Tuesday, April 29, 2014 6:46 PM