ContextMenu on tap instead of tap and hold

好久不见. 提交于 2019-11-29 01:50:33

You could add GestureListener to the Border and subscribe to the Tap event. In the event handler, you get the ContextMenu for the Border and set IsOpen to true if it doesn't have a logical parent.

<Border BorderThickness="3" Padding="6">
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
    </toolkit:GestureService.GestureListener>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="item1" Click="Item1_Click" />
            <toolkit:MenuItem Header="item2" Click="Item2_Click" />
            <toolkit:MenuItem Header="item3" Click="Item3_Click" />
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
    <TextBlock Text="Tap" />
</Border>

private void GestureListener_Tap(object sender, GestureEventArgs e)
{
    Border border = sender as Border;
    ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
    if (contextMenu.Parent == null)
    {
        contextMenu.IsOpen = true;
    }
}

If you want a context menu for your application, then the ContextMenu and ContextMenuService are the best approach to take because it is standard throughout third party and pre-installed applications. Users already understand the 'tap-and-hold' gesture, so working around that will be counter-intuitive.

If (for whatever reason) you must initiate a ContextMenu from a single tap, then you can always customize the source code for ContextMenu.cs from the Silverlight Toolkit so that instead of hooking the Hold event it hooks the Tap event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!