WPF (MVVM):连接 viewModel 以在 Window 的代码隐藏中查看?

发布于 2024-08-05 10:31:29 字数 297 浏览 2 评论 0原文

我一直在接触 WPF 的 MVVM 模式,一切都按预期工作。

但是我正在将视图模型从窗口后面的代码连接到视图数据上下文。

果然,MVVM 的目的是摆脱 Windows 中的代码......它做得很好,所以我只剩下 2 行简单的代码来将我的数据上下文连接到我的视图模型。

还有其他选择吗?

或者只用 2 行代码来连接窗口代码中的 Viewmodel 是否可以接受?

是否可以通过 XAML 将视图模型连接到视图?我认为可能是(不确定)-但这会引起问题吗?推荐吗?

真的很感激任何帮助吗?

I have been getting my feet wet with MVVM pattern for WPF and everything is working as it should..

But i am wiring up the viewmodel to my view datacontext from my codebehind of my window.

Sure enough the point of MVVM is to get rid of code behind in windows... which it does great, so i am left with 2 simple lines to wire up my datacontext to my viewmodel.

Is there an alternative?

Or is it acceptable to have only 2 lines to wire up the Viewmodel in my window code behind?

Is it possible to wire up the viewmodel to the view via XAML? I think it probably is (unsure) - But would this cause issues? Is it recommended?

Really appreciated any help?

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

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

发布评论

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

评论(1

你的呼吸 2024-08-12 10:31:29

是的,您可以通过 DataTemplates 通过 XAML 将 DataContext 连接到视图。您可以在优秀且强烈推荐的文章WPF Apps With The模型-视图-视图模型设计模式,作者:Josh Smith。

Yes, you can wire up the DataContext to the view through XAML via DataTemplates. You can see an example of this in the excellent and highly recommended article WPF Apps With The Model-View-ViewModel Design Pattern by Josh Smith.

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