Search code examples
c#wpfmvvmwpftoolkit

WPF Toolkit DateTimePicker make Time Textbox un-editable


I am trying to make the time picker part of the Calender to have the readonly textbox. Because by default it allows user to type invalid time and throws index out of range. I like to just allow time picker from up down arrows and disable typing in that textbox.

I have tried using TimePickerVisibility to hidden but it hides the time picker.

enter image description here


Solution

  • UPDATE 1
    I have just now noticed that you are using DateTimePicker and not TimePicker,
    so you need to Set TimePicker of the DateTimePicker to not AllowTextInput in this way:

        <xctk:DateTimePicker HorizontalAlignment="Left" Margin="67,200,0,0" VerticalAlignment="Top" Width="228">
            <xctk:DateTimePicker.Resources>
                <Style TargetType="{x:Type xctk:TimePicker}">
                    <Setter Property="AllowTextInput" Value="False"/>
                </Style>
            </xctk:DateTimePicker.Resources>
        </xctk:DateTimePicker>
    

    UPDATE2
    If you want to disable all TextInputs, so that user won't be able to input neither Date nor Time you can do it in this way:

    <xctk:DateTimePicker HorizontalAlignment="Left" Margin="67,200,0,0" VerticalAlignment="Top" Width="228">
        <xctk:DateTimePicker.Resources>
            <Style TargetType="{x:Type xctk:DateTimePicker}">
                <Setter Property="AllowTextInput" Value="False"/>
            </Style>
            <Style TargetType="{x:Type xctk:TimePicker}">
                <Setter Property="AllowTextInput" Value="False"/>
            </Style>
        </xctk:DateTimePicker.Resources>
    </xctk:DateTimePicker>
    

    If you will use standard DatePicker the same is done in the following way. For standard WPF DatePicker you need to set DatePickerTextBox to ReadOnly. You can do it in this way:

    <DatePicker HorizontalAlignment="Left" Margin="138,82,0,0" VerticalAlignment="Top">
        <DatePicker.Resources>
            <Style TargetType="DatePickerTextBox">
                <Setter Property="IsReadOnly" Value="True"/>
            </Style>
        </DatePicker.Resources>
    </DatePicker>