4 Answers, 1 is accepted
The table options are only shown in the context menu when you right-click in a table. This is so, because the commands that they trigger need an instance of a table - it has to be clear which table you want to insert a row in, delete a row of, etc. The commands themselves cannot be executed, so even if you add them in the context menu, the context menu items will be disabled.
If you decide to add other ContextMenuGroups to the context menu, you can do that by handling the Showing event of the context menu like this:
(
this
.editor.ContextMenu
as
ContextMenu).Showing+=
new
EventHandler<ContextMenuEventArgs>(ContextMenu_Showing);
void
ContextMenu_Showing(
object
sender, ContextMenuEventArgs e)
{
e.ContextMenuGroupCollection.Add(CreateCustomMenuGroup());
}
Best wishes,
Iva
the Telerik team
However, we would like to also have an Insert Table command available at all times when the user right-clicks. I have implemented your suggestions about listening to the context menu showing event, and adding a custom context menu group. This code does put an "Insert Table" menu option in my context menu; however, it does nothing.
I would like to "steal" the built in RadRichTextBox.Commands.InsertTableCommand logic; and just put that into a context menu. Can I even do that?
Thanks for your help!
Stacey
FYI: here is my code to add my custom menu group to the context menu; again, this is adding the insert table group successfully, but when clicked, does nothing:
private
void
radRichTextBoxEdit_Loaded(
object
sender, RoutedEventArgs e)
{
RadEn_USDictionary radDict =
new
RadEn_USDictionary();
((DocumentSpellChecker)
this
.radRichTextBoxEdit.SpellChecker).AddDictionary(radDict, CultureInfo.InvariantCulture);
ContextMenu contextMenu = (ContextMenu)
this
.radRichTextBoxEdit.ContextMenu;
contextMenu.Showing +=
new
System.EventHandler<ContextMenuEventArgs>(contextMenu_Showing);
}
private
void
contextMenu_Showing(
object
sender, ContextMenuEventArgs e)
{
e.ContextMenuGroupCollection.Add(CreateCustomMenuGroup());
}
private
ContextMenuGroup CreateCustomMenuGroup()
{
RadMenuItem item =
new
RadMenuItem();
item.Header =
"Insert Table"
;
item.Command =
this
.radRichTextBoxEdit.Commands.InsertTableCommand;
ContextMenuGroup group =
new
ContextMenuGroup();
group.Add(item);
return
group;
}
The InsertTableCommand is not executed, because it requires a parameter indicating the number of rows and columns you want to have in the table. If you want to insert a table that has 3 rows and 2 columns for example, you need to set the CommandParameter property of the item like this:
item.CommandParameter =
new
InsertTableCommandParameter(3, 2);
However, since there is no way to know how many rows and columns the user would like to have in the table, a better approach would be to use ShowInsertTableDialogCommand, that would show a dialog where you can set the initial row and column number. The only line you need to change is where you set the command:
item.Command =
this
.radRichTextBoxEdit.Commands.ShowInsertTableDialogCommand;
If you have other questions, do not hesitate to contact us again.
Kind regards,
Iva
the Telerik team