wolfman1001 wolfman1001 - 19 days ago 18
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.

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


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

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