Say I have the following data template:

    <DataTemplate x:Key="ItemTemplate">
        <StackPanel>
            <Grid Height="95" Width="446" HorizontalAlignment="Left" ShowGridLines="false" RenderTransformOrigin="0.3,0.526">
                <Grid.RowDefinitions>
                    <RowDefinition Height="100"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding categoryimage}" Height="100" Grid.Row="0" Grid.Column="0" />
                <TextBlock Text="{Binding categoryname}" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="8,0,-46,0" FontSize="26.667"/>
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu Name="MyContextMenu">
                        <toolkit:MenuItem Header="Delete console" Click="contextMenuAction_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
            </Grid>
        </StackPanel>
    </DataTemplate>

I want to handle when the context menu item is clicked. How would I access the list item as a class object (if this makes sense?)

I've tried the following code, however it's giving me a NullReferenceException exception:

Private Sub contextMenuAction_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

    Dim c As classes.consoles = listBoxview.SelectedItem
    MessageBox.Show(c.categoryname)

End Sub

enter image description here

有帮助吗?

解决方案

You might be able to get the parent item using the Visual Tree Helper, as detailed in the following thread :-

Context menu selected item wp7

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top