ListView UWP 上的右键菜单

Sux*_*orm 1 c# win-universal-app windows-10 uwp

我有一个通过鼠标右键调用上下文菜单的代码。

private void GridColections_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {

        MenuFlyout myFlyout = new MenuFlyout();
        MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "OneIt" };
        MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "TwoIt" };
        myFlyout.Items.Add(firstItem);
        myFlyout.Items.Add(secondItem);
        FrameworkElement senderElement = sender as FrameworkElement;
        myFlyout.ShowAt(senderElement);
    }
Run Code Online (Sandbox Code Playgroud)

但菜单出现在我的列表视图的中心。不在我点击鼠标的地方。如何修复它?

lin*_*exi 6

如果您希望弹出窗口显示在鼠标单击点处,则可以ShowAt(UIElement,Point)使用ShowAt(FrameworkElement).

可以在您的点击点中显示浮出控件的代码。

     private void GridColection_OnRightTapped(object sender, RightTappedRoutedEventArgs e)
     {
         MenuFlyout myFlyout = new MenuFlyout();
         MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "OneIt" };
         MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "TwoIt" };
         myFlyout.Items.Add(firstItem);
         myFlyout.Items.Add(secondItem);

         //if you only want to show in left or buttom 
         //myFlyout.Placement = FlyoutPlacementMode.Left;

         FrameworkElement senderElement = sender as FrameworkElement;

         //the code can show the flyout in your mouse click 
         myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
     }
Run Code Online (Sandbox Code Playgroud)