简体   繁体   English

ListView UWP上的右键菜单

[英]RightClick menu on ListView UWP

I have a code that calls the context menu by right mouse button. 我有一个通过鼠标右键调用上下文菜单的代码。

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);
    }

But the menu appears in the center of my listview. 但是菜单出现在我的列表视图的中心。 Not at the place where I clicked on the mouse. 不在我单击鼠标的地方。 How to fix it? 如何解决?

If you want the Flyout show at your mouse click point,and you can use the ShowAt(UIElement,Point) rather ShowAt(FrameworkElement) . 如果要在鼠标单击点显示ShowAt(UIElement,Point)显示,则可以使用ShowAt(UIElement,Point)而不是ShowAt(FrameworkElement)

The code that can show the Flyout in your Click point. 可以在点击点显示弹出按钮的代码。

     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));
     }

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

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