Skip to content

Latest commit

 

History

History
67 lines (58 loc) · 2.51 KB

kb-datetimepicker-replace-contextmenu-with-radcontextmenu.md

File metadata and controls

67 lines (58 loc) · 2.51 KB
title description type page_title slug position tags ticketid res_type
How to replace the default ContextMenu with RadContextMenu in RadDateTimePicker
Using RadContextMenu instead of ContextMenu in RadDateTimePicker.
how-to
Show RadContextMenu rather than ContextMenu in RadDateTimePicker
kb-datetimepicker-replace-contextmenu-with-radcontextmenu
contextmenu, radcontextmenu, raddatetimepicker, datetimepicker
1427861
kb

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]({%slug common-visual-tree-helpers%}#childrenoftypeextensions) 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.

[XAML]

{{region kb-datetimepicker-replace-contextmenu-with-radcontextmenu-0}} <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" /> {{endregion}}

[C#]

{{region kb-datetimepicker-replace-contextmenu-with-radcontextmenu-1}} 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);
    }
}

{{endregion}}

As an alternative approach, you can also [Edit the ControlTemplate]({%slug styling-apperance-editing-control-templates%}) of the RadDateTimePicker.