我可以在MVVM应用程序中的代码中执行此操作吗? [英] Can I do this in code behind in a MVVM application?

查看:85
本文介绍了我可以在MVVM应用程序中的代码中执行此操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVVM相对较新,似乎没有就.xaml.cs代码隐藏文件中是否允许某些代码达成共识,或者根本没有。我将非常感谢您对特定问题的指导:



我在MVVM模式中有一个Ribbon应用程序。功能区有6个选项卡,每个选项卡都有自己的命令集和 ViewModel 中自己的唯一类。功能区下方是 DataGrid ,它绑定到每个选项卡的不同 DataTable 。每个选项卡的 DataTable 都位于自己的类中。这意味着每次用户单击其他选项卡时, DataGrid DataContext 必须更改。如果我在窗口的代码隐藏文件中更改 DataContext ,那么所有这些都很有效。您无法绑定到 DataContext ,因此您无法在 ViewModel中设置 DataContext ,至少不是我能想到的。



现在我的问题:允许在后面的代码中设置 DataGrid DataContext ,还是MVVM众神钉死我?

解决方案

不应在代码隐藏中设置DataContext。

将datagrid的源设置为集合。

I am relatively new with MVVM and there does not seem to be total consensus on whether some code is allowed in the .xaml.cs code behind file, or none at all. I will appreciate guidance with a specific problem:

I have a Ribbon application in the MVVM pattern. The ribbon has 6 tabs, each with its own set of commands and its own unique class in the ViewModel. Underneath the ribbon is a DataGrid that is bound to a different DataTable for each tab. The DataTable for each tab resides in its own class. This means the DataContext of the DataGrid must change each time the user clicks on a different tab. All of this works great, provided I change the DataContext in the window's code behind file. You cannot bind to the DataContext, so you cannot set the DataContext in the ViewModel, at least not as far as I could figure out.

Now my question: Is it permissible to set the DataGrid DataContext in code behind, or will the MVVM gods crucify me?

解决方案

No DataContext should not be set in the code-behind.
Set the datagrid's source to a collection.


这篇关于我可以在MVVM应用程序中的代码中执行此操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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