[UWP] Variable Number of MenuFlyoutItems

Md. Niaz Mahmud 171 Reputation points

Is it possible to implement MenuFlyout with variable number of MenuFlyoutItem/ToggleMenuFlyoutItem in UWP??? If yes, how is it? How to get the individual click event?

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

Accepted answer
  1. Roy Li - MSFT 32,721 Reputation points Microsoft Vendor


    Welcome to Microsoft Q&A!

    Based on @Castorix31 's reply, you could check the MenuFlyout Class or take a look at the simplified code here:


       <Rectangle x:Name="Rect1" Height="100" Width="200"   
                   Stroke="Black" StrokeThickness="1" Fill="White">  
                    <MenuFlyout x:Name="RectangleColorMenu"/>  


    To add items

     // Create the menu item.  
                var newMenuItem = new MenuFlyoutItem();  
                newMenuItem.Text ="YourItemName";  
                newMenuItem.Click += (s, e1) =>  
                    Rect1.Fill = new SolidColorBrush(Colors.Red);  
                // Add the item to the menu.  
                // Sort the menu so it's always consistent.  
                var orderedItems = RectangleColorMenu.Items.OrderBy(i => ((MenuFlyoutItem)i).Text).ToList();  
                foreach (var item in orderedItems)  

    To remove items:

     // Get any menu items to remove and remove them.  
                var items = RectangleColorMenu.Items.Where(i => ((MenuFlyoutItem)i).Text == "YourItemName");  
                foreach (MenuFlyoutItem item in items)  

    Thank you.

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 82,316 Reputation points

    In the doc, there is a sample at MenuFlyout Class:

    (part : This example shows how you can add and remove menu items at runtime based on changing conditions in your app.)

    0 comments No comments