WPF 日历:绑定到 MVVM 命令? [英] WPF Calendar: Binding to MVVM commands?

查看:43
本文介绍了WPF 日历:绑定到 MVVM 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 WPF 日历连接到 MVVM 视图模型.我不确定如何将日期选择和月份更改绑定到 MVVM ICommand 对象.例如,要处理选定的日期更改,我将在 XAML 中将什么对象绑定到视图模型中的相应命令属性?

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?

据我所知,我被代码隐藏中的事件处理困住了.例如,看起来检测日期选择更改的唯一方法是编写 SelectedDatesChanged 事件处理程序.我可以从事件处理程序调用视图模型中的命令属性,如下所示:

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();
}

但我感觉到这种方法的代码味道,除非没有更好的方法可用.谁能告诉我如何直接从 XAML 将 WPF 日历绑定到 MVVM 命令?有没有比我正在采取的方法更好的方法?谢谢你的帮助.

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.

推荐答案

您的 VM 中是否没有 DateTime 属性绑定到日历并引发 PropertyChanged?如果是这样,您不能在内部监视属性更改或在 setter 中编写代码以便您知道视图何时更改了其值?

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?

这篇关于WPF 日历:绑定到 MVVM 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆