tags:

views:

45

answers:

1

I am wiring up a WPF calendar to an MVVM view model. I'm not sure how to bind date selections and month changes to MVVM ICommand objects. For example to process a selected date change, what object would I bind in XAML to the appropriate command property in my view model?

As nearly as I can tell, I am stuck with event handling in code-behind. For example, it looks like the only way to detect a date selection change is to write a SelectedDatesChanged event handler. I can invoke the command property in my view model from the event handler, like this:

private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
    var viewModel = (CalendarViewModel) DataContext;
    viewModel.GetDateNotes.Execute();
}

But I sense a code smell to that approach, unless there is no better approach available. Can anyone tell me how to bind a WPF Calendar to MVVM commands directly from XAML? Is there a better approach than the one I am taking? Thanks for your help.

A: 

Wouldn't you have a DateTime property in your VM that is bound to the calendar and that raises PropertyChanged? If so, can't you watch for property changed internally or write code in your setter so that you know when the view has changed its value?

MarkB
Of course! I got so wrapped up in commands that I couldn't see the forest for the trees. Thanks!
David Veeneman