SuxoiKorm
SuxoiKorm

Reputation: 179

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?

Upvotes: 1

Views: 2359

Answers (1)

lindexi
lindexi

Reputation: 4327

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

Upvotes: 6

Related Questions