Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Comments
Sergey Alexandrovich Kryukov 17-Feb-15 18:07pm    
:-)

1 solution

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900