Hello,
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:
Xaml:
<Rectangle x:Name="Rect1" Height="100" Width="200"
Stroke="Black" StrokeThickness="1" Fill="White">
<Rectangle.ContextFlyout>
<MenuFlyout x:Name="RectangleColorMenu"/>
</Rectangle.ContextFlyout>
</Rectangle>
Code-behind
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.
RectangleColorMenu.Items.Add(newMenuItem);
// Sort the menu so it's always consistent.
var orderedItems = RectangleColorMenu.Items.OrderBy(i => ((MenuFlyoutItem)i).Text).ToList();
RectangleColorMenu.Items.Clear();
foreach (var item in orderedItems)
{
RectangleColorMenu.Items.Add(item);
}
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)
{
RectangleColorMenu.Items.Remove(item);
}
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.