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
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>
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;
}
}
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.