Search code examples
uwpuwp-xaml

Replace Default Menu Flyout with costum one in RichEditBox in uwp


I wanted to replace the Default Menu Flyout with my custom Menu Flyout but below code is not working, still the default context menu is showing instead of custom one

public sealed partial class MainPage : Page
    {
        RichEditBox box1;
        MenuFlyout myContextMenu;
        public MainPage()
        {
            this.InitializeComponent();

            box1 = new RichEditBox()
            {
                Width = 500,
                Height = 30,
                BorderBrush = new SolidColorBrush(Colors.Blue),
                BorderThickness = new Thickness(1, 1, 1, 1)
            };

            myContextMenu = new MenuFlyout();
            MenuFlyoutItem cutMenuItem = new MenuFlyoutItem();
            cutMenuItem.Text = "Cut";
            myContextMenu.Items.Add(cutMenuItem);

            MenuFlyoutItem copyMenuItem = new MenuFlyoutItem();
            copyMenuItem.Text = "Copy";
            myContextMenu.Items.Add(copyMenuItem);

            MenuFlyoutItem pasteMenuItem = new MenuFlyoutItem();
            pasteMenuItem.Text = "Paste";
            myContextMenu.Items.Add(pasteMenuItem);

            MenuFlyoutItem selectAllMenuItem = new MenuFlyoutItem();
            selectAllMenuItem.Text = "Select All";
            myContextMenu.Items.Add(selectAllMenuItem);

            box1.ContextFlyout = myContextMenu;

            Content = box1;
        }
}

Solution

  • Ok as you mentioned in the comments, it only happens when you select something:

    There are two types of flyouts in a RichEditBox, first the default flyout and second the selection flyout. Here is the code to change both

    box1.SelectionFlyout = box1.ContextFlyout = myContextMenu;