简体   繁体   中英

Restricting C# DatePicker

I need to give my users a way of selecting any valid Saturday in a WPF app. Right now, I just use the DatePicker and error if the day of the week isn't Saturday.

DateTime selectedDate = Convert.ToDateTime(datePicker1.ToString());
if (selectedDate.DayOfWeek != DayOfWeek.Saturday)
{
    MessageBox.Show("Please pick a Saturday");
}

I'd rather do it in the control and simply slide back to the next Saturday or make Sun-Fri unclickable. This should be easy, but I'm drawing a blank.

I think you want to look at the BlackoutDates property. You can subscribe to the DisplayDateChanged Event to catch when the user switches months and reinitialize the list of BlackoutDates for that new month.

Depending on how clear you need to be to the user, you could use subtle color highlighting of the control (forecolor, backcolor) to indicate an error. Then use the message box for if they try to ignore the warning.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM