I don't want to put it all in one ViewModel, I want to have one viewModel per tabItem.
I created a mainViewModel for the window containing the TabControl, where I have this property currentViewModel,pointed to a default value on the mainViewModel constructor
public MainViewModel()
{
currentViewModel = "viewModel1";
}
When a user clicks on another tabItem this executes
currentViewModel = "viewModel2";
and of course the set accessor has the onPropertyChanged method
public String currentViewModel
{
get { return _currentViewModel; }
set
{
_currentViewModel = value;
OnPropertyChanged("currentViewModel");
}
}
Another two viewModels (viewModel1,viewModel2) each one determines the functionality of one of the tabItems I want to switch between.
Now on my Main.xaml I want to bind my dataContext to firstly the MainViewModel and then to the currentViewModel property. so that whenever user clicks on a tabItem the currentViewModel propert get updated and the dataContext point to the corresponding viewmodel.
if (_fieldOfProperty != value)
block around those two lines in the setter. – H.B.