2

I have the following DatePicker control:

<Grid x:Name="LayoutRoot" Background="White">
       <DatePicker Margin="2" Grid.Column="1" VerticalAlignment="Center"/>
</Grid>

Is it possible to allow user only to set numeric input? Or how to disable the Textbox input in case it's not possible to acheive

MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 1
    [Allow only numeric values](http://stackoverflow.com/a/1268648/4632606) and adapt it to the textbox of the DatePicker And [Disable the text input](http://stackoverflow.com/questions/5402529/remove-textinput-from-datepicker) – ZwoRmi Jan 04 '16 at 09:51

3 Answers3

3

Try this one:

 <DatePicker Margin="2" Grid.Column="1" VerticalAlignment="Center">
            <DatePicker.Resources>
                <Style TargetType="DatePickerTextBox">
                    <Setter Property="IsReadOnly" Value="True"/>
                </Style>
            </DatePicker.Resources>
        </DatePicker>
</Grid>
Edgaras
  • 154
  • 9
3

This code only allows to enter numeric values in DatePicker

XAML:

<DatePicker Margin="2" Grid.Column="1"
            PreviewTextInput="phoneNumber_PreviewTextInput" 
            VerticalAlignment="Center"/>

Code behind

private void phoneNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    char character = Convert.ToChar(e.Text);
    if (char.IsNumber(character))
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}
steinar
  • 9,383
  • 1
  • 23
  • 37
Justin CI
  • 2,693
  • 1
  • 16
  • 34
0

Actually I appreciate your answers and I really enjoy them but I would like the solution I found, in fact I used the Regex and below is the used code :

private bool IsTextAllowed(string text)
{
     Regex regex = new Regex("[^0-9/]+"); 
     return !regex.IsMatch(text);
}

private void DatePicker_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
     e.Handled = !IsTextAllowed(e.Text);
}
MRebai
  • 5,344
  • 3
  • 33
  • 52