Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datepicker to disable user input

Tags:

wpf

datepicker

I have a datepicker, but it is allowing me to enter any text. I want to disable user from entering text. User should be allowed to select date from the calendar.

<Window x:Class="MyTestForDatePicker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <!--<Grid>-->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <DatePicker Grid.Column="0" Grid.Row="0" Margin="2" Name="MySelectedDate"     VerticalContentAlignment="Center" ></DatePicker>
        <DatePickerTextBox Grid.Column="1" Grid.Row="3" Margin="2" Name="SelectedDate"   IsReadOnly="True"></DatePickerTextBox>
        <Button Grid.Column="1" Grid.Row="1" Margin="100,102,203,154" VerticalContentAlignment="Center" Name="Ok" Click="Ok_Click_1"/>
    </Grid>

like image 621
user2418423 Avatar asked May 24 '13 17:05

user2418423


2 Answers

seems like there is no property on DatePicker to make the TextBox read only.

Try the below style setting

        <DatePicker x:Name="MyDatePicker">
            <DatePicker.Resources>
                <Style TargetType="DatePickerTextBox">
                    <Setter Property="IsReadOnly" Value="True"/>
                </Style>
            </DatePicker.Resources>
        </DatePicker>
like image 181
krishnaaditya Avatar answered Sep 18 '22 15:09

krishnaaditya


Add the property

Focusable = "False"

to your datepicker. This should not allow user to enter any string in datepicker textbox

<DatePickerTextBox Grid.Column="1" Grid.Row="3" Margin="2" Name="SelectedDate"   IsReadOnly="True" Focusable="False"></DatePickerTextBox>
like image 22
nandu Avatar answered Sep 20 '22 15:09

nandu