question

zipswich avatar image
0 Votes"
zipswich asked ·

How to show MenuFlyout in code?

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.

uwpuwp-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RoyLi-MSFT avatar image
1 Vote"
RoyLi-MSFT answered ·

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 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot, Roy. It works like a charm. I use the following to show at the clicked button:

 menuFlyoutContext.ShowAt((sender as Button));

0 Votes 0 · ·