Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p>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. </p> <pre><code> public override ICommand SaveCommand { get { return _currentWorkspace != null ? _currentWorkspace.SaveCommand : new RelayCommand ( null, param =&gt; false); } } </code></pre> <p>and as in the original code, where it keeps track of current workspace, made sure I notified the subsystem of the change</p> <pre><code> public ObservableCollection&lt;WorkspaceViewModel&gt; Workspaces { get { if (_workspaces == null) { _workspaces = new ObservableCollection&lt;WorkspaceViewModel&gt;(); _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"); } } </code></pre> <p>that's it, WPF took care of the rest (ie, enabling, disabling button depending on the validations)! </p> <p>Again, thanks for your tip Paul.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload