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?