0

假设我有以下数据模板:

    <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>

我想在单击上下文菜单项时进行处理。我如何将列表项作为类对象访问(如果这有意义?)

我尝试了以下代码,但是它给了我一个 NullReferenceException 异常:

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

在此处输入图像描述

4

1 回答 1

1

您可能可以使用 Visual Tree Helper 获取父项,如以下线程中所述:-

上下文菜单选择的项目 wp7

于 2012-05-16T16:20:05.377 回答