在 MVVM 架构中应在何处以及如何管理选定的项目?

发布于 2024-09-06 06:46:01 字数 352 浏览 5 评论 0原文

我有一个视图,允许用户选择一些数据:例如一些日期,并执行需要这些数据的命令。

因此,在我的命令中,我应该引用所选日期,但是使该日期转到命令所在的 ViewModel 端的最佳实践是什么:

  • 在 ViewModel 中添加 SelectedDate 依赖属性并将我的视图绑定在其上,并通过“@this.SelectedDate”在我的命令中引用它(@this是对当前ViewModel的引用),
  • 让视图通过命令的“执行”方法的“参数”传输它,并引用日期与“DateTime selectedDate = (DateTime)parameter;”,
  • 任何其他解决方案...

提前致谢。

I have a view that allows the user to select some data : some dates for example, and executes a command that needs these data.

So in my command I should have a reference to the selected date, but what is the best practice to make this date go to the ViewModel side where the command lives :

  • to add a SelectedDate dependency property in the ViewModel and bind my view on it, and reference it in my command via "@this.SelectedDate" (with @this a reference to the current ViewModel),
  • to let the view transmit it through the "parameter" of the "Execute" method of the command, and reference the date with "DateTime selectedDate = (DateTime)parameter;",
  • any other solution...

Thanks by advance.

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

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

发布评论

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

评论(3

阳光的暖冬 2024-09-13 06:46:01

我绝对会让 SelectedDate 成为视图模型的依赖属性。

我还会让命令从视图模型中获取 SelectedDate 。视图没有理由知道任何相关信息。

I'd make SelectedDate a dependency property of the view model, absolutely.

I'd also make the command get the SelectedDate from the view model. There's no reason for the view to know anything about that.

素食主义者 2024-09-13 06:46:01

对于第一部分,是的,我认为 SelectedDate 应该是 ViewModel 的 (INotify) 属性。

我对你的指挥部应该从哪里获取这些信息没有强烈的意见,我认为这个属性还可以。

To the first part, Yes I think the SelectedDate should be an (INotify) property of your ViewModel.

I don't have a strong opinion on where your Command should obtain this information, I think the property is OK.

不一样的天空 2024-09-13 06:46:01

WPF 应用程序框架 (WAF)BookLibrary 示例应用程序strong> 展示了一种使用 MVVM 处理所选项目的方法:ViewModel 有一个绑定到 View 的“SelectedBook”属性。

The BookLibrary sample application of the WPF Application Framework (WAF) shows a way to handle the selected item with MVVM: The ViewModel has a 'SelectedBook' property which is bound to the View.

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