Disable past dates in datapicker wpf it is possible

I have a datapicker in wpf and I have to disable past dates. I am using the MVVM pattern. Is it possible?

How do you do this?

+4
source share
4 answers

You can use the DisplayDateStart and DisplayDateEnd DatePicker . They are dependency properties, so you can provide them through your DataContext using MVVM. Here's the documentation:

+7
source

Additionally, for Rick's answer, DisplayDateStart and DisplayDateEnd affect only the calendar, this does not prevent the user from entering a valid date outside this range.

To do this, you can create an exception to the setter in the associated property in the ViewModel or if you use IDataErrorInfo, return a validation error message through this [string columnName]

ExceptionValidationRule:

 <Binding Path="SelectedDate" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <ExceptionValidationRule /> </Binding.ValidationRules> </Binding> 
+3
source

This might be a tidier solution if your delayed date ranges include the constants you want to keep in xaml.

0
source

You must set the DisplayDateStart attribute with the current date.

 <DatePicker Name="dt_StartDateFrom" DisplayDateStart="{x:Static sys:DateTime.Today}"> </DatePicker> 

Make sure you install

XMLNS: SYS = "CLR Names: system; assembly = mscorlib"

in the <UserControl> tag to be able to use the sys: parameter sys:

PS To disable future dates, you can use the DisplayDateEnd attribute

0
source

Source: https://habr.com/ru/post/1338214/


All Articles