This is a migrated thread and some comments may be shown as answers.

Adding to Context menu problem with multiple RichTextBox instances

2 Answers 131 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Ralph
Top achievements
Rank 1
Ralph asked on 22 May 2014, 09:11 PM
Our WPF application contains multiple RadRichTextBox instances. When right clicking in one of the instances I expected the ContextMenu_Showing event to get fired for only the RadRichTextBox control that I right clicked. This is not the case. 

The method gets fired for each instance of the RadRichTextBox.  As a result, instead of getting one "View Source" context menu item inserted into the context menu, I get one for each RadRichTextBox control.

I am trying to implement the modification of the RadRichTextBox's context menu as indicated at the following link:
http://www.telerik.com/help/wpf/radrichtextbox-features-context-menu.html

I also modified the example as indicated in the following blog to reference the correct ContextMenu:
http://blogs.telerik.com/xamlteam/posts/10-11-17/customizing-radrichtextbox-s-contextmenu-and-selectionminitoolbar.aspx

In the Telerik public constructor I have the following code:

            Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu =
                (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)this.radRichTextBox.ContextMenu;
            contextMenu.Showing += this.ContextMenu_Showing;

I also have the following methods:

        private void ContextMenu_Showing(object sender, Telerik.Windows.Controls.RichTextBoxUI.Menus.ContextMenuEventArgs e)
        {
            RadMenuItem makeViewSourceMenuItem = new RadMenuItem()
            {
                Header = "View Source"
            };
            makeViewSourceMenuItem.Click += MakeViewSourceMenuItem_Click;
            ContextMenuGroup customContextMenuGroup = new ContextMenuGroup();
            customContextMenuGroup.Add(makeViewSourceMenuItem);
            e.ContextMenuGroupCollection.Add(customContextMenuGroup);
        }        private void MakeViewSourceMenuItem_Click(object sender, RadRoutedEventArgs e)
        {
            FrmHtmlSource dialog = new FrmHtmlSource(_originalText);
            dialog.ShowDialog();
        }

All the examples I have seen involve just one instance of the RadRichTextBox.  Is there anything special I need to do to support multiple instances or is this a limitation with the RadRichTextBox control.

2 Answers, 1 is accepted

Sort by
0
Accepted
Boby
Telerik team
answered on 26 May 2014, 08:34 AM
Hi Ralph,
This is known peculiarity of the RadRichTextBox, caused by the fact that ContextMenu is instantiated by MEF, which by default caches the instance for reuse - meaning that all RadRichTextBox instances use one and the same context menu.

As a workaround, I would suggest you to try one of the two approaches explained in this forum thread.

Don't hesitate to contact us if you have other questions.


Regards,
Boby
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Ralph
Top achievements
Rank 1
answered on 27 May 2014, 09:28 PM
Thanks Boby,  you suggestion fixed the problem.
Tags
RichTextBox
Asked by
Ralph
Top achievements
Rank 1
Answers by
Boby
Telerik team
Ralph
Top achievements
Rank 1
Share this question
or