WPF DatePicker Control Display Dates - wpf

I'm using WPF with MVVM implementation, and have a WPF DatePicker control. I've set the default date on my property using DateTime.Now, but I would like to disable all the Dates on the DatePicker control that fall before the current date so that the User cannot select any earlier dates.
I've tried setting this in xaml using FallbackValue={x:Static sys:DateTime.Now} in my SelectedDate property, and {x:Static sys:DateTime.Now} in most of the other properties (from another source) but the dates before the current dates are still displayed and still selectable.
Is there an easy way to accomplish this?

Maybe you're looking BlackoutDates property?
Here is more info of using the DatePicker control (including BlackoutDates):
WPF Toolkit: Calendar & DatePicker Walkthrough
For example if you want to set your blackout dates from 1/1/2000 to present day you can do:
<Calendar.BlackoutDates>
<CalendarDateRange Start="1/1/2000" End="{x:Static System:DateTime.Today}" />
</Calendar.BlackoutDates>
Where System namespace is defined as
xmlns:System="clr-namespace:System;assembly=mscorlib"

You can use DisplayDateStart and DisplayDateEnd properties to select what dates should be displayed in the control or if you want something more complex you can use BlackOutDates to select exactly what dates you don't want to be selectable.

CalendarDateRange cdr = new CalendarDateRange(DateTime.MinValue, DateTime.Today);
myCalendar.BlackoutDates.Add(cdr);

You can use
MyDatePicker.BlackoutDates.AddDatesInPast();
See https://msdn.microsoft.com/en-us/library/system.windows.controls.calendarblackoutdatescollection.adddatesinpast(v=vs.110).aspx

Related

Specify user input date format in WPF datepicker

I would like users to have an ability to type in dates in datepicker control. The two formats allowed are MMDDYYYY or MMDDYY, by default it is not possible to do it without slash
Dealing with this myself. Decided I wanted more of a "maskedtextbox" serving as my textbox versus the datepickertextbox. I had to include WPFToolkit for the maskedtextbox control. Then, I created a new class that inherits from Datepicker and added a few dependencyproperties. From there I created a resourcedictionary for the XAML of how to display this new custom date picker. You can build You'll lose the watermark but you gain a mask which I think is more user friendly.
You won't be able to accomplish what you are looking for without a custom control since Datepicker is tightly synced together where the selected Calendar date is an actual date and SelectedDate has to also be a date and the format MMDDYYYY isn't considered a date.
I managed to accomplish that easily with telerik's RadDatePicker. Setting AllowParsingWithoutSeparator="True" property resolved my issue. The other solution is to have a textbox with mask.

How to display Date as MMM-DD dateFormat in WPF DatePicker

I have WPF DatePicker In My UserControl and I want to display date as MMM-DD format.
I am using MVVM Pattern.
Any One faced this issue.
There us a lot of topic about that whit more or less hacking of the control but may be you could try the DateTimePicker from the WPF Toolkit which has 2 properties 'Format' and 'FormatString'
Like it's describe in this another post.
Set Format=Custom
And FormatString="youre custom format" like "dd::MMM::yyyy hh-mm-ss" or what ever you need

Mark calendar blackout dates from context menu command

I am using MVVM and trying to mark the blackout dates of a calendar from its Calendar.ContextMenu using a command. I am trying to pass the calendar control as command parameter, but I am unsuccessful.
I would appreciate some help
That's bad MVVM, the ViewModel should not have any implementation knowledge of the UI. By passing a UIElement to the ViewModel you are breaking the pattern.
Instead, pass the SelectedDate as the command parameter and bind the BlackoutDates property to a collection of DateTimes in the ViewModel?

WinForms - A datepicker with date AND time?

I just cant find how can i make the Windows Forms DatePicker to have both date and time for the user to select. It doesn't?
The only way i see is to put two Datepickers one with the date and one with the time and combine the DateTime object that i need... that's the only way?
Is the Winforms DateTimePicker not available for some reason? If it is available, you can create one, choose 'Custom' for the Format property then type yyyy-MM-dd hh:mm:ss or whatever you want in the CustomFormat property.

How to set Silverlight 5 CurrentCulture & CurrentUICulture separately on DatePicker & TimePicker Control?

I have set two different cultures to the thread as:
Thread.CurrentThread.CurrentCulture = "fi-FI"
Thread.CurrentThread.CurrentUICulture = "en-Us"
But the calender text is shown in Finnish culture.
I need to keep the Text(Translations) based on CurrentUICulture and Text Format based on CurrentCulture.
Can you please suggest how can it be done for it?
That is not possible. The Calendar control only reacts to the CurrentCulture value. CurrentUICulture is used by the ResourceManager to load up different strings, images etc. used in the UI, but that is not where the day/month names are stored. They are part of the .NET framework itself. To get the functionality you want you will have to write your own implementation of the DatePicker and/or TimePicker controls. Perhaps you could download the code for the Silverlight Toolkit and use that as a base for your own implementation. What you are looking for is basically a way of overriding the names of months and days manually. Maybe it is even possible to extend the controls and add that functionality on top, but I doubt it.

Resources