How to add context menu dynamically to the RichTextBox on right click.
In this article I am going to discuss How to add context menu dynamically to the RichTextBox? Please check below to implement it.
1- Add mouse up event in RichText Box
<RichTextBox Name="RichTextBoxCommand" MouseUp="RichTextBoxCommand_MouseUp"></RichTextBox>
2- Dynamically add context menu on MouseUp event.
if (e.RightButton == Mouse.RightButton) { ContextMenu contextMenu = new ContextMenu(); MenuItem selectAllmenu = new MenuItem(); selectAllmenu.Header = "Select All"; selectAllmenu.Click += (o, a) => { RichTextBoxCommand.SelectAll(); }; MenuItem copymenu = new MenuItem(); copymenu.Header = "Copy"; copymenu.Click += (o, a) => { RichTextBoxCommand.Copy(); }; contextMenu.Items.Add(selectAllmenu); contextMenu.Items.Add(copymenu); RichTextBoxCommand.ContextMenu = contextMenu; }
Here I added two sub menu Select All and Copy. As you right click on Rich text box. both menus will display there.
0 comments :
Post a Comment