简体   繁体   English

WP7-ContextMenu列表框项目单击事件

[英]WP7 - ContextMenu listbox item click event

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: 我尝试了以下代码,但是它给了我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

在此处输入图片说明

You might be able to get the parent item using the Visual Tree Helper, as detailed in the following thread :- 您可能可以使用Visual Tree Helper获取父项,如以下线程中所述:

Context menu selected item wp7 上下文菜单选择项wp7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM