user2868614
user2868614

Reputation: 85

WPF C# DatePicker open calendar 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 calendar.

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

Upvotes: 5

Views: 7639

Answers (3)

Yahya
Yahya

Reputation: 511

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

Upvotes: 5

Witold
Witold

Reputation: 29

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;
}

Upvotes: 2

Mark Feldman
Mark Feldman

Reputation: 16148

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

Upvotes: 7

Related Questions