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 |
Product | RadDateTimePicker for WPF |
How to replace the default ContextMenu with RadContextMenu in RadDateTimePicker.
- Add the RadContextMenu as a StaticResource in xaml.
- Handle the Loaded event of the RadDateTimePicker.
- 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.
- Set the RadContextMenu to the RadWatermarkTextBox using the RadContextMenu.SetContextMenu() method.
{{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}}
{{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.