I would like to add a context menu to a RadRichTextBox with options to delete a table, insert/delete a table column and insert/delete a table row. But I want these context menu options to be visible for the user only when he has selected (part of) a table in the RadRichTextBox.
Is there a way to detect via the code if the user has selected a table, a table column or a table row?
Thanks,
Sodi
13 Answers, 1 is accepted
The easiest way will be to use RadRichTextBox's default context menu, which includes this functionality by default. If you want to customize the default table editing commands, you can do it by providing it your custom content builder to the context menu:
public
MainPage()
{
InitializeComponent();
ContextMenu contextMenu = (ContextMenu)
this
.radRichTextBox1.ContextMenu;
contextMenu.ContentBuilder =
new
CustomMenuBuilder(
this
.radRichTextBox1);
}
public
class
CustomMenuBuilder : ContextMenuContentBuilder
{
private
RadRichTextBox radRichTextBox;
public
CustomMenuBuilder(RadRichTextBox radRichTextBox)
:
base
(radRichTextBox)
{
this
.radRichTextBox = radRichTextBox;
}
protected
override
ContextMenuGroup CreateClipboardCommands()
{
return
base
.CreateClipboardCommands();
// or just
//return null;
}
protected
override
ContextMenuGroup CreateSpellCheckingSuggestions()
{
return
base
.CreateSpellCheckingSuggestions();
// or just
//return null;
}
protected
override
ContextMenuGroup CreateTextEditCommands()
{
return
base
.CreateTextEditCommands();
// or just
//return null;
}
protected
override
ContextMenuGroup CreateTableCommands()
{
return
new
ContextMenuGroup()
{
new
RadMenuItem()
{
Header =
"Delete Table"
,
Command =
this
.radRichTextBox.Commands.DeleteTableCommand
},
new
RadMenuItem()
{
Header =
"Insert Table Column"
,
Command =
this
.radRichTextBox.Commands.InsertTableColumnToTheRightCommand
},
new
RadMenuItem()
{
Header =
"Delete Table Column"
,
Command =
this
.radRichTextBox.Commands.DeleteTableColumnCommand
},
new
RadMenuItem()
{
Header =
"Insert Table Row"
,
Command =
this
.radRichTextBox.Commands.InsertTableRowBelowCommand
},
new
RadMenuItem()
{
Header =
"Delete Table Row"
,
Command =
this
.radRichTextBox.Commands.DeleteTableRowCommand
}
};
}
}
Regards,
Boby
the Telerik team
Unfortunately, the default context menu of the RadRichTextBox does not work in my application for some reason.
I added references to the dll's you mentioned. I also added a reference to the Telerik.Windows.Documents.dll. In the XAML, I mentioned the following namespace declaration:
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Then I created the RadRichTextBox in the XAML, like so:
<
telerik:RadBusyIndicator
IsBusy
=
"{Binding IsLoadingDetail}"
Style
=
"{StaticResource BusyIndicatorStyle}"
>
<
Grid
x:Name
=
"LayoutRoot"
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
=
"Auto"
/>
<
RowDefinition
Height
=
"Auto"
/>
<
RowDefinition
Height
=
"*"
/>
</
Grid.RowDefinitions
>
...
<
telerik:RadRichTextBox
x:Name
=
"radRichTextbox"
Grid.Row
=
"1"
Background
=
"Beige"
></
telerik:RadRichTextBox
>
...
</
Grid
>
</
telerik:RadBusyIndicator
>
But when I run my application and rightclick on the RadRichTextBox, the context menu does not appear. Any ideas?
Thanks
Sodi
It is quite strange that the context menu does not appear in your application. Are you using library caching? If that is the case, you can find an explanation and a solution in this forum thread.
To the list of required assemblies that Boby mentioned previously, I would add Telerik.Windows.Controls.RibbonBar, as it is also needed by the RichTextBoxUI assembly.
Let us know if you manage to resolve the issue. Otherwise, we would greatly appreciate any further details about your application. A sample project would be best, as it would help us track down the problem more easily.
Iva
the Telerik team
ContextMenu contextMenu = (ContextMenu)
this
.radRichTextBox1.ContextMenu;
Problem is that ContextMenu is System.Windows.Controls.ContextMenu, which does not have a property of ContextBuilder. Should I be referencing the ContextMenu differently? For example, when you mouse over ContextMenu, is it a System.Windows.Controls.ContextMenu, or something else?
The default ContextMenu of RadRichTextBox is of type:
Telerik.Windows.Documents.UI.Extensibility.IContextMenu
and you need to cast it to:
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu
The actual type of the context menu is the second one, but as we support custom context menus, the type of the ContextMenu property is that of the interface.
I hope that answers your question. Greetings,
Iva
the Telerik team
Code in initializer:
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)Editor.ContextMenu; // Editor is my RadRichTextBox
contextMenu.ContentBuilder = new CopyOnlyContextMenu(Editor);
Code to make the context menu:
public class CopyOnlyContextMenu : ContextMenuContentBuilder
{ private RadRichTextBox radRichTextBox; public CopyOnlyContextMenu(RadRichTextBox radRichTextBox) : base(radRichTextBox) { this.radRichTextBox = radRichTextBox; } protected override ContextMenuGroup CreateClipboardCommands() { return new ContextMenuGroup() { new RadMenuItem() { Header = "Copy", Command = this.radRichTextBox.Commands.CopyCommand } }; } protected override ContextMenuGroup CreateSpellCheckingSuggestions() { return null; } protected override ContextMenuGroup CreateTableCommands() { return null; } protected override ContextMenuGroup CreateTextEditCommands() { return null; } }
Thank you for providing the code of your implementation.The Copy command requires that the selection is not empty and this is the only case when the RadMenuItem should appear disabled. I created a sample application using the class and everything worked as expected. You can find it attached to this post.
I have also kept another custom context menu builder in the project, which shows how you can add images to the RadMenuItems (it is commented though).
I hope this helps.
Iva
the Telerik team
The only explanation that comes to mind is that you have several editors and are assigning the content builder without instantiating a new ContextMenu for each one. Normally, only one instance of ContextMenu is created for all RadRichTextBoxes in the application and that may cause the behavior you are observing.
If you do have several editors, you have to proceed as in the code-snippet below:
rtb1.ContextMenu =
new
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder =
new
CustomMenuBuilder(rtb1) };
rtb2.ContextMenu =
new
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder =
new
CustomMenuBuilder(rtb2) };
rtb3.ContextMenu =
new
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder =
new
CustomMenuBuilder(rtb3) };
If that is not the case, we would greatly appreciate a sample demo illustrating the problem.
Iva
the Telerik team
Cheers
Thank you for the feedback. We will make sure to include this item in our online documentation, so that no such misunderstandings occur.
All the best,Iva
the Telerik team
Now I'd like to localize this commands. Some months ago I found a resource file with many of the text resources found in telerik controls, I translated it currently I set Telerik the LocalizationManager to use the translated resources file. But I suspect that resources used in RichTextBox are not present in this resources file. Do RichTextBox controls use LocalizationManager to resolve their strings? If they do, where can I get the RichTextBox resource to translate them? Thanks.
The strings that RadRichTextBox uses are included in the Strings.resx file, distributed with the source of the controls. Most of them start with "Documents_", so you can check if they are not included in the file you've got.
In any case, there is a resource file with all strings that RadRichTextBox and its default UI use here. For more detailed information on the localization of the control you can refer to this article.
Iva
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>