简体   繁体   中英

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) .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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