I knew whatever I had done earlier was a workaround and it was dirty. As I understood more about MVVM, I kept re-factoring the code. Here's what I did.
Added the ICommand Proerty for SaveCommand on MainWindowViewModel, and bound it to the toolbutton on Main Window. It just delegates the call to the currently active WorksSpaceViewModel's SaveCommand.
public override ICommand SaveCommand
{
get
{
return _currentWorkspace != null ? _currentWorkspace.SaveCommand : new RelayCommand ( null, param => false);
}
}
and as in the original code, where it keeps track of current workspace, made sure I notified the subsystem of the change
public ObservableCollection<WorkspaceViewModel> Workspaces
{
get
{
if (_workspaces == null)
{
_workspaces = new ObservableCollection<WorkspaceViewModel>();
_workspaces.CollectionChanged += OnWorkspacesChanged;
CollectionViewSource.GetDefaultView(_workspaces).CurrentChanged += new EventHandler(MainWindowViewModel_CurrentChanged);
}
return _workspaces;
}
}
private void MainWindowViewModel_CurrentChanged(object sender, EventArgs e)
{
CurrentWorkspace = CollectionViewSource.GetDefaultView(_workspaces).CurrentItem as WorkspaceViewModel;
OnPropertyChanged("SaveCommand");
}
public WorkspaceViewModel CurrentWorkspace
{
get { return _currentWorkspace; }
set
{
_currentWorkspace = value;
OnPropertyChanged("CurrentWorkspace");
}
}
that's it, WPF took care of the rest (ie, enabling, disabling button depending on the validations)!
Again, thanks for your tip Paul.