5

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 calendar.

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

3 Answers 3

7
<DatePicker HorizontalAlignment="Left" VerticalAlignment="Top">
    <DatePicker.Resources>
        <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"/>
        </Style>
    </DatePicker.Resources>
</DatePicker>

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.

2
  • Thank you very much for the solution! I will use the WPF visualizer if I run into a similar problem. Commented Dec 1, 2013 at 23:44
  • my datepicker was acting very odd because of opening the calendar on hover , and i searched the whole internet and finally found this , you saved my day thank you !
    – The Doctor
    Commented Jun 21, 2022 at 19:34
5

The answer of Mark Feldman is partially right, he had not answer to how clicking on the textbox gonna open the calendare popup!

You should hook the template of the DatePicker and enlarge the size of the button so it will overlap the textbox: then you are done, when you click on the textbox it's the button that get the event see below a part of the modified template of DatePicker:

<DatePickerTextBox Grid.Row="0"  Grid.Column="0" x:Name="PART_TextBox" Focusable="{TemplateBinding Focusable}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
<Button Grid.Row="0"  Grid.Column="0" x:Name="PART_Button"  Foreground="{TemplateBinding Foreground}" Focusable="False" >
    <Button.Template>
        <ControlTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Border Background="Transparent"/>
                <Button Grid.Column="1" HorizontalAlignment="Left" Foreground="{TemplateBinding Foreground}" Template="{StaticResource DropDownButtonTemplate}"  VerticalAlignment="Top" Width="20" Margin="3,0,3,0" />
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

Dont forget to add the style of DatePickerTextBox described in the answer of Mark Feldman

1
  • thanks for this. I've been struggling with this one, nice solution. Commented Oct 30, 2020 at 9:06
2

You can open calendar this way :

<DatePicker Name="datePicker" Focusable="False">
     <DatePicker.Resources>
          <Style TargetType="DatePickerTextBox">
                <EventSetter Event="MouseLeftButtonUp" Handler="OnMouseLeftButtonUp" />
          </Style>
     </DatePicker.Resources>
</DatePicker>
private void OnMouseLeftButtonUp(object sender, RoutedEventArgs e)
{
    datePicker.IsDropDownOpen = true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.