I'm using a DatePicker in a WPF window and I need to execute a command to refresh the MVVM data when the user tries to open the calendar window. The DatePicker does not have a Command property.
Is there a way to accomplish this?
Thanks.
I'm using a DatePicker in a WPF window and I need to execute a command to refresh the MVVM data when the user tries to open the calendar window. The DatePicker does not have a Command property.
Is there a way to accomplish this?
Thanks.
You could create an attached behavior for that or use an available framework like this to link events with commands. From that you can expose and bind your command in your viewmodel and place your update logic there.
Also this question really belongs to these kind of questions.
OK, if you really want to attach a command to an event you can use a pattern like this: http://petebarber.blogspot.com/2011/06/getting-wpf-sizechanged-events-at-start.html
Basically you define (in a separate file) an attached property which hooks up to an event. So in your case this would be the CalendarOpened event of the DatePicker. So the attached property would be called CalendarOpenedCommand and you would then use the property on your actual DatePicker like this:
<DatePicker mvvm:CalendarOpenedCommand="{Binding MyCalendarOpenedCommand}" ...
However, I personally think this is overkill if this is the only place you will use this feature. It might upset some MVVM purists, but in this case I would just put a one line event handler for CalendarOpened in the code behind, which could then call a method (or even a command if you prefer) on the view model to notify it of the calendar opening. It could then do its business and update the bound properties for DisplayDateStart and DisplayDateEnd (as I suggested in the comments above) accordingly.