user2868614 user2868614 - 9 months ago 76
C# Question

WPF C# DatePicker open calender on click

I am adding a WPF DatePicker control to my form and it works fine. But I don't want the user to be able to type in a type in the 'Select a date' textbox. I want that to be readonly and when they click the textbox it just opens the calender.

I wasn't sure if there was an option for this in the properties? I couldn't find anything...

Answer Source
<DatePicker HorizontalAlignment="Left" VerticalAlignment="Top">
        <Style TargetType="DatePickerTextBox">
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="Text" Value=" "/>
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="IsHitTestVisible" Value="False"/>

In future you can use the WPF visualizer to see which child controls a top-level control is using (in this case DatePickerTextBox) and then apply a style and/or template to that type in the resources section like I've done here.