New to Telerik UI for WPF? Download free 30-day trial

How to replace the default ContextMenu with RadContextMenu in RadDateTimePicker

Environment

Product RadDateTimePicker for WPF

Description

How to replace the default ContextMenu with RadContextMenu in RadDateTimePicker.

Solution

  1. Add the RadContextMenu as a StaticResource in xaml.
  2. Handle the Loaded event of the RadDateTimePicker.
  3. In the Loaded event, use the ChildrenOfType method to find the RadWatermarkTextBox inside the RadDateTimePicker and set its ContextMenu property to null.
  4. Set the RadContextMenu to the RadWatermarkTextBox using the RadContextMenu.SetContextMenu() method.

<Grid x:Name="grid"> 
    <Grid.Resources> 
        <telerik:RadContextMenu x:Key="ContextMenu"> 
            <telerik:RadMenuItem Command="ApplicationCommands.Cut" /> 
            <telerik:RadMenuItem Command="ApplicationCommands.Copy"  /> 
            <telerik:RadMenuItem Command="ApplicationCommands.Paste" /> 
        </telerik:RadContextMenu> 
    </Grid.Resources> 
    <telerik:RadDateTimePicker  Width="200" Height="25" Foreground="Green" Loaded="RadDateTimePicker_Loaded" />  
</Grid> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
        InitializeComponent(); 
    } 
 
    private void RadDateTimePicker_Loaded(object sender, RoutedEventArgs e) 
    { 
        var waterMarkTextBox = (sender as RadDateTimePicker).ChildrenOfType<RadWatermarkTextBox>().FirstOrDefault(); 
 
        waterMarkTextBox.ContextMenu = null; 
        var contextMenu = this.grid.FindResource("ContextMenu") as RadContextMenu; 
        RadContextMenu.SetContextMenu(waterMarkTextBox, contextMenu); 
    } 
} 

As an alternative approach, you can also Edit the ControlTemplate of the RadDateTimePicker.

In this article