简体   繁体   English

listview的上下文菜单不会触发RightTapped事件

[英]context menu for listview does not fire the RightTapped event

I have been working with c# for some time now but surprisingly I have never dealt with context menus before. 我已经使用c#已有一段时间了,但是令人惊讶的是,我之前从未处理过上下文菜单。 I have a listView control in my universal windows 8.1 app. 我的通用Windows 8.1应用程序中有一个listView控件。 Now I am trying to get a context menu to popup for each item in the listView (they are all the same type of object and are added to the list as the user adds entries). 现在,我试图为上下文列表中的每个项目弹出一个上下文菜单(它们都是相同类型的对象,并在用户添加条目时添加到列表中)。 I have run into several problems with this and have looked at code examples and they seem to be leading in different directions. 我遇到了一些问题,并查看了代码示例,它们似乎朝着不同的方向发展。 Firstly when I right click on an item in the list it does not fire the ListView_RightTapped event. 首先,当我右键单击列表中的项目时,它不会触发ListView_RightTapped事件。

 <ListView x:Name="lstvwHours" HorizontalAlignment="Left" Height="264" Margin="427,77,0,0" VerticalAlignment="Top" Width="357" RightTapped="lstvwHours_RightTapped">

Secondly in Microsoft's context menu code example they say to use the PopupMenu class but in other code I've seen it coded into the XAML. 其次,在Microsoft的上下文菜单代码示例中,他们说使用PopupMenu类,但在其他代码中,我看到了它已编码到XAML中。

And lastly After the one context menu button is clicked I want it to fire a delete method. 最后,单击一个上下文菜单按钮后,我希望它触发一种删除方法。 private async void lstvwHours_RightTapped(object sender, 私人异步void lstvwHours_RightTapped(对象发送方,

RightTappedRoutedEventArgs e)
{
    var menu = new PopupMenu();
    menu.Commands.Add(new UICommand("Delete"/*do I put the method to call here?*/));
    var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
}

Here's an example. 这是一个例子。

In this case you can wire-up the commands that get invoked from your menuitem onto your view-model. 在这种情况下,您可以将从菜单项调用的命令连接到视图模型。

<ListView>
    <ListViewItem Content="One">
        <ListViewItem.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Insert"
              Command="{Binding DataContext.InsertQuery, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                <MenuItem Header="Delete" 
              Command="{Binding DataContext.DeleteQuery, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
            </ContextMenu>
        </ListViewItem.ContextMenu>
    </ListViewItem>
</ListView>

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

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