How to show MenuFlyout in code?

Hong 1,211 Reputation points
2020-02-25T21:53:02.807+00:00

The MenuFlyout shows up perfectly upon right-clicking the ListView

 <ListView Name="lvDeviceTiles"  
       ItemsSource="{x:Bind listDeviceItems}"  
       DataContext="{x:Bind}"  
       Margin="0">  
     <!--ContextFlyout shows up automatically after right-click-->  
     <ListView.ContextFlyout>  
         <MenuFlyout   
             x:Name="menuFlyoutContext"  
             Opening="menuFlyoutContext_Opening">  
           ...    
         </MenuFlyout>  
     </ListView.ContextFlyout>  
 </ListView>  

How can I show it in C# code?
I tried:

FlyoutBase.ShowAttachedFlyout(lvDeviceTiles);  
lvDeviceTiles.ContextFlyout.ShowAt(lvDeviceTiles);  
lvDeviceTiles.ContextFlyout.ShowAt(myButton);  

None of them work.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 33,471 Reputation points Microsoft Vendor
    2020-02-26T06:50:06.477+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I have to say that you are in the right direction. To show the MenuFlyout control, you could just call FlyoutBase.ShowAt Method to do that. But please note that MenuFlyout control is derived from FlyoutBase. So you just need to call MenuFlyout.ShowAt method in your scenario.

    Like this:

            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                menuFlyoutContext.ShowAt(lvDeviceTiles);  
            }  
    

    Thank you!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.