当用户在WPF中打开DatePicker日历时如何执行命令?

发布于 2025-01-08 17:35:05 字数 120 浏览 1 评论 0原文

我在 WPF 窗口中使用 DatePicker,当用户尝试打开日历窗口时,我需要执行命令来刷新 MVVM 数据。 DatePicker 没有 Command 属性。

有办法做到这一点吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2025-01-15 17:35:05

好的,如果您确实想将命令附加到事件,您可以使用如下模式: http://petebarber.blogspot.com/2011/06/getting-wpf-sizechanged-events-at-start.html

基本上,您定义(在单独的文件中)一个连接到事件的附加属性。因此,在您的情况下,这将是 DatePicker 的 CalendarOpened 事件。因此,附加属性将被称为 CalendarOpenedCommand,然后您将在实际的 DatePicker 上使用该属性,如下所示:

<DatePicker mvvm:CalendarOpenedCommand="{Binding MyCalendarOpenedCommand}" ...

但是,我个人认为,如果这是您将使用此功能的唯一地方,那么这有点过分了。这可能会让一些 MVVM 纯粹主义者感到不安,但在这种情况下,我只需在后面的代码中放置 CalendarOpened 的一行事件处理程序,然后它可以在视图模型上调用一个方法(甚至是命令,如果您愿意)来通知它日历打开。然后,它可以执行其业务并相应地更新 DisplayDateStart 和 DisplayDateEnd 的绑定属性(正如我在上面的评论中建议的那样)。

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.

吃颗糖壮壮胆 2025-01-15 17:35:05

您可以为此创建附加行为或使用可用框架,例如 this 将事件与命令链接起来。由此,您可以在视图模型中公开并绑定命令,并将更新逻辑放置在那里。

另外,这个问题确实属于这些类型的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文