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

Customized context menu

7 Answers 134 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Christoph asked on 15 May 2012, 03:40 PM
Hi

I want to extend the default rich text box
context menu.
I'd like to add a new entry with a submenu entries,
when seelcting such an entry I want to fire some
command thatz inserts fragments into the rrtb doc.
How am I gonna do it?
From what I've read in the forums I need to
implement a custom "IContextMenuContentBuilder"
Is that correct?
And how can I do that without replacing the original ctx menu?

Chris

7 Answers, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 16 May 2012, 09:34 AM
Hi Chris,

You can find more information on customizing the ContextMenu in this article. Basically, you can create a custom ContextMenuContentBuilder and overload the methods for creating clipboard commands, text edit commands, etc. The method must return a RadMenuGroup with the items you would like to include.

Here is an example that adds a RadMenuItem for InsertPicture in the text-edit commands group:

protected override ContextMenuGroup CreateTextEditCommands()
{
    ContextMenuGroup baseGroup = base.CreateTextEditCommands();
    baseGroup.Add(new RadMenuItem()
    {
        Command = this.radRichTextBox.Commands.InsertPictureCommand,
        Header = "Insert picture",
        Icon = new Image(){Source = new BitmapImage(new Uri("/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/Picture.png", UriKind.Relative))                  
        }
    });
    return baseGroup;
}

I hope this answers your question. Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Christoph
Top achievements
Rank 1
answered on 23 May 2012, 03:37 PM
Hi Iva,

Thanks for the nice example. Got it working!

Chris
0
Christoph
Top achievements
Rank 1
answered on 24 May 2012, 05:54 PM
One question left:

If I want hierarchical menus with (optionally) more than one
level of submenus, do I have to construct the submenus
manually by setting a collection of child RadMenuItems as
a parent RadMenuItems ItemsSource.

like

Parent > Child 1
               Child 2
               Child 3 >  Child 3.1
                               Child 3.2

new RadMenuItem {Header = "parent",
    ItemsSource = new RadMenuItem []
    {
        new RadMenuItem {Header = "child 1"},
        new RadMenuItem {Header = "child 2"},
        new RadMenuItem {Header = "child 2",
        ItemsSource = new RadMenuItem []  {
             new RadMenuItem {Header = "child 3.1"},
             new RadMenuItem {Header = "child 3.2"},
         }
       }
    }
};


Or can I somehow work with (Hierachical) DataTemplates
and recursive datastructures where the data class has a
Children collection and a Header string (and an Icon and a Command)?

So in the override of ContextMenuBuilder.Construct
I just need to create a  ContextmenuGroup add it one (or more) RadMenuItem(s)
assign it a (Hierachical) ItemTemplate
and assign its ItemsSource property a collection of my Tree-data items
and thus the SubMenu gets autocreated based on the data tree

Hope you roughly understood what I mean.

EDIT:
I've seen the these 4 template key properties in RadMenuItem:
TopLevelHeaderTemplateKey, TopLevelItemTemplateKey,
SubmenuItemTemplateKey and SubmenuHeaderTemplateKey
I guess, these are what I need to use.
Is there some information available how to use this properties
to build hierarchical menus?


Thanks and Regards
Chris





0
Christoph
Top achievements
Rank 1
answered on 25 May 2012, 10:32 AM
Hi

I got it resolved by taking a look at your source code.
In the "CreateTableCommands()" implementation of the default Telerik
ContextMenuContentBuilder  a hierarchical structure is constrcuted
code behind.
Basically it's all about adding child RadMenuItems to a parent RadMenuItems
Items collection.
I adopted this mechanism to build a menu with submenus based
on my recursive data structure. Works fine
0
Iva Toteva
Telerik team
answered on 28 May 2012, 12:52 PM
Hello Chris,

Thank you for the follow-up. We are glad to hear that you have been able to achieve the desired behavior yourself.

If you need further assistance, do not hesitate to contact us again.

Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Robert
Top achievements
Rank 1
answered on 22 Jun 2012, 09:46 AM
Hi,

I'm trying to override the context menu:
ContextMenu contextMenu = (ContextMenu)this.radRichTextBox.ContextMenu;
contextMenu.ContentBuilder = new CustomMenuBuilder(this.radRichTextBox);


but I get the following error:

Error 1 'System.Windows.Controls.ContextMenu' does not contain a definition for 'ContentBuilder' and no extension method 'ContentBuilder' accepting a first argument of type 'System.Windows.Controls.ContextMenu' could be found (are you missing a using directive or an assembly reference?)


0
Robert
Top achievements
Rank 1
answered on 22 Jun 2012, 10:13 AM
Sorry, it was because I was using:

using System.Windows.Controls;


I removed it from my code and it works as it should.
Tags
RichTextBox
Asked by
Christoph
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Christoph
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Share this question
or