wolfman1001 wolfman1001 - 1 year ago 294
C# Question

UWP position flyout at mouse cursor

I have a grid with a load of textblocks inside and a flyout with various options attached to the grid.

<MenuFlyoutItem x:Name="EditButton" Text="Edit" Click="EditButton_Click"/>
<MenuFlyoutItem x:Name="DeleteButton" Text="Delete"/>

The problem is that the flyout will appear in the same fixed spot somewhere in the middle of the grid or I can set it programmatically to appear at an element. I want it to appear wherever the mouse was right clicked. Is this possible or am I going about this the wrong way?

Answer Source

I don't know how are you showing the Flyout, but in my app, I use the RightTapped event of my ListView and following code in the RightTapped event handler to achieve the same thing as you want.

private void MyListView_RightTapped(object sender, RightTappedRoutedEventArgs e)
    var tappedItem     = (UIElement)e.OriginalSource;
    var attachedFlyout = (MenuFlyout)FlyoutBase.GetAttachedFlyout(MyListView);

    attachedFlyout.ShowAt(tappedItem, e.GetPosition(tappedItem));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download