New to Telerik UI for WPF? Start a free 30-day trial
Deleting a Content Control in a Context Menu
Updated on Apr 1, 2026
Environment
| Product | RichTextBox for UI for WPF |
| Version | 2026.1.211 |
Description
I need to delete a content control from the RichTextBox when using a custom context menu in the ContextMenu_Showing event.
This knowledge base article also answers the following questions:
- How to add a "Remove Content Control" option inside a RichTextBox context menu?
- How to delete a content control from a document in RichTextBox for WPF?
- How to handle content control deletion through a custom menu in RichTextBox?
Solution
To delete a content control, use the SelectAnnotationRange method of the Selection property of the RadDocument class. Pass the SdtRangeStart instance of the clicked content control as a parameter. Then, invoke the Delete method of the ActiveDocumentEditor property of the RadRichTextBox control.
Follow these steps:
- Handle the
ContextMenu_Showingevent to add a menu option for removing the content control. - Implement logic to detect if the caret is over a content control.
- Use the
SelectAnnotationRangemethod to select the content control and theDeletemethod to remove it.
Here is an example implementation:
csharp
private void ContextMenu_Showing(object sender, Telerik.Windows.Controls.RichTextBoxUI.Menus.ContextMenuEventArgs e)
{
if (IsCaretOverContentControl())
{
RadMenuItem menuItem = new RadMenuItem()
{
Header = "Remove Content Control"
};
menuItem.Click += MenuItem_Click;
ContextMenuGroup contextMenuGroup = new ContextMenuGroup();
contextMenuGroup.Add(menuItem);
e.ContextMenuGroupCollection.Add(contextMenuGroup);
}
}
private void MenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
SdtRangeStart sdtRangeStart = GetContainingSdtRangeStart(this.rtb.Document.CaretPosition);
if (sdtRangeStart == null || sdtRangeStart.SdtEnd == null)
return;
this.rtb.Document.Selection.SelectAnnotationRange(sdtRangeStart);
this.rtb.ActiveDocumentEditor.Delete(false);
}
private bool IsCaretOverContentControl()
{
var caretPosition = this.rtb.Document.CaretPosition;
SdtRangeStart sdtStart = GetContainingSdtRangeStart(caretPosition);
return sdtStart != null;
}
private SdtRangeStart GetContainingSdtRangeStart(DocumentPosition caretPosition)
{
var paragraph = caretPosition.GetCurrentParagraph();
if (paragraph == null)
return null;
var element = caretPosition.GetCurrentInline();
DocumentElement current = element as DocumentElement;
while (current != null)
{
if (current is SdtRangeEnd)
return null;
if (current is SdtRangeStart sdtStart)
return sdtStart;
current = current.PreviousSibling;
}
return null;
}
Key Details:
- The
ContextMenu_Showingevent customizes the context menu by adding a "Remove Content Control" option. - The
MenuItem_Clickmethod performs the deletion by selecting and removing the content control.