Mark calendar blackout dates from context menu command - wpf

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?

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.

WPF DatePicker Control Display Dates

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

Formatting contents of a datagrid

I have a wpf datagrid (.NET 4.0) that contains raw data from a database, and some of the fields need to be formatted to display in a meaningful manner, for example 3 fields containing Year / Month / Day could be formatted together to produce a properly formatted date field.
Is there a nice way of doing this ?
Separate your View/XAML from your ViewModel, and keep your formatting in the ViewModel as this doesnt belong in the View!
then you can bind your date field to your datagrid and not mess up your XAML or code-behind.
If you look for a good framework to help you keep this design I recommend looking at MVVM Light or Caliburn.Micro
You scould not bInd directly you data, but you have to create a function on your model and then bind the function on you view

Where and how should selected items be managed in a MVVM architecture?

I have a view that allows the user to select some data : some dates for example, and executes a command that needs these data.
So in my command I should have a reference to the selected date, but what is the best practice to make this date go to the ViewModel side where the command lives :
to add a SelectedDate dependency property in the ViewModel and bind my view on it, and reference it in my command via "#this.SelectedDate" (with #this a reference to the current ViewModel),
to let the view transmit it through the "parameter" of the "Execute" method of the command, and reference the date with "DateTime selectedDate = (DateTime)parameter;",
any other solution...
Thanks by advance.
I'd make SelectedDate a dependency property of the view model, absolutely.
I'd also make the command get the SelectedDate from the view model. There's no reason for the view to know anything about that.
To the first part, Yes I think the SelectedDate should be an (INotify) property of your ViewModel.
I don't have a strong opinion on where your Command should obtain this information, I think the property is OK.
The BookLibrary sample application of the WPF Application Framework (WAF) shows a way to handle the selected item with MVVM: The ViewModel has a 'SelectedBook' property which is bound to the View.

WPF Binding to a non-changing property

I'm using the MVVM pattern and I have a POCO (in my Model) with a Start Date property.
I want to show the elapsed time since the start date in a control on a WPF window/user control...
I don't see how I can bind a ModelView property to a UI control and have it update this duration automatically...can anyone suggest a way?
I could use something (a timer or a thread) to update a duration property on my ModelView but I just don't see any other way because as I understand it the UI will only update when a property value changes. However the start date on my POCO isn't changing it's just the elapsed time that's changing which is a calculated value.
Am I missing something?
You're on the right track. Have a look at the Presentation Model pattern on Martin Fowler's page.
The basic idea is to build a model for the UI (ViewModel) and have the UI just sync up with it. Every bit of information to be displayed in the UI, should have a corresponding field or property in the ViewModel (although they may be retrieved or derived from values in the Model) .. the ViewModel makes it easy to store the View State/Session State (such as the current selection of items in a UserList) which is not present the in the Model class behind.
Since you want to show the 'elapsed time since' value in the UI, your ViewModel should have a property caled ElapsedTimeSince. Your WPF View has a control which is data-bound to this property.
Now as per your need, ensure you have a thread/timer event that re-evaluates the property value periodically using the current time and the Model's StartDate property. Your UI should reflect the updated value.

Resources