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
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 >>
Thanks for the nice example. Got it working!
Chris
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
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
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.
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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?)
using
System.Windows.Controls;
I removed it from my code and it works as it should.