开发者

WPF Calendar: Binding to MVVM commands?

开发者 https://www.devze.com 2023-01-12 07:48 出处:网络
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 wo

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开发者_如何学Python?

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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号