I learnt about a nice side effect of view models, tab controls and data context

I am currently working on a new piece of functionality for an application. Up until now it had assumed that there would only be one instance of a grid in a panel however, as often happens, the requirement has arisen to support multiple grids each hosted in a separate tab – built dynamically at run-time, using MEF.

This has meant refactoring the current solution and splitting it into two – a user control for the grid part and a user control to host the tabs.

In the host control there is a toolbar that will be the same for all tabs but needs to be linked to the current tab. This existed in the current solution and up until now the toolbar data-binds to commands in the view model for the grid (we are using MVVM).

My initial thought was to pull the commands out into the main host for the tabs.

However one of the nice side effects of the approach is that if I change the DataContext of the toolbar to the DataContext of the current grid as the toolbar selection changes it will all work for free and there is no need for the commands to be moved up or to worry about which is the selected tab etc. Nice.