wpfteleriktelerik-datepicker

How to auto-close a telerik RadDateTimePicker on selected date


The control is declared as follows :

 <telerik:RadDateTimePicker InputMode="DateTimePicker"
                            IsTabStop="False"
                            Height="36"
                            Focusable="False"
                            Validation.ErrorTemplate="{x:Null}"
                            SelectionOnFocus="SelectAll"
                            x:Name="OutwardStartDate"
                            BorderThickness="2,2,2,2"
                            SelectedValue="{Binding OutwardDepartureDate, Mode=TwoWay, ValidatesOnDataErrors=true,
                                                                     NotifyOnValidationError=true}" >

I've failed to find any XAML attributes in order to auto-close this control when a date is selected. I've stumble upon this post on the official website, however the question is dated from 5 years ago and none of the current answers solved my issue.


Solution

  • You can simply hook to the SelectionChanged event.

    In your XAML as follows :

    <telerik:RadDateTimePicker InputMode="DateTimePicker"
                               SelectionChanged="RadDateTimePicker_SelectionChanged"
                               IsTabStop="False"
                               Height="36"
                               Focusable="False"
                               Validation.ErrorTemplate="{x:Null}"
                               SelectionOnFocus="SelectAll"
                               x:Name="OutwardStartDate"
                               BorderThickness="2,2,2,2"
                               SelectedValue="{Binding OutwardDepartureDate,
                                                   Mode=TwoWay,
                                                   ValidatesOnDataErrors=true,
                                                   NotifyOnValidationError=true}">
    

    and in the code behind change the IsDropDownOpen property :

    private void RadDateTimePicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems != null)
        {
            var dateTimePicker = sender as RadDateTimePicker;
            dateTimePicker.IsDropDownOpen = false;
        }
    }