Reputation: 2512
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
Upvotes: 0
Views: 820
Reputation: 3294
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
Upvotes: 1