WPF (MVVM):连接 viewModel 以在 Window 的代码隐藏中查看?
我一直在接触 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以通过 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.