/// <summary> /// Helper function that tries to remove a ReadDocumentViewModel. /// </summary> /// <param name="rdvm">The ReadDocumentViewModel</param> private void tryRemoveRDVM(ReadDocumentViewModel rdvm) { if (rdvm == null) { return; } // Stop reading if it was reading the closing document if (_readDoc == rdvm) { _reader.StopReading(); } // Remove the viewmodel if it exists if (_rdvms.Contains(rdvm)) { _rdvms.Remove(rdvm); } }
public MainWindowViewModel(ReadDocumentManager docsMan) { // Add commandbindings _reader = new DocumentReader(); _reader.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_reader_PropertyChanged); _commandBindings = new CommandBindingCollection(); CommandBinding NewCmdBinding = new CommandBinding( ApplicationCommands.New, (t, e) => _docsMan.Add()); this.CommandBindings.Add(NewCmdBinding); CommandBinding PlayCmdBinding = new CommandBinding( MediaCommands.Play, PlayCmdExecuted, PlayCmdCanExecute); this.CommandBindings.Add(PlayCmdBinding); CommandBinding PauseCmdBinding = new CommandBinding( MediaCommands.Pause, PauseCmdExecuted, PauseCmdCanExecute); this.CommandBindings.Add(PauseCmdBinding); CommandBinding StopCmdBinding = new CommandBinding( MediaCommands.Stop, (t, e) => _reader.StopReading()); this.CommandBindings.Add(StopCmdBinding); _docsMan = docsMan; _rdvms = new ObservableCollection <ReadDocumentViewModel>(); //The easiest way to republate the viewmodel is to call the delegate that manages the changes MainWindowViewModel_CollectionChanged(_docsMan.Documents, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); // To keep our ViewModelCollection up to date we need to listen for changes ((INotifyCollectionChanged)(_docsMan.Documents)).CollectionChanged += new NotifyCollectionChangedEventHandler(MainWindowViewModel_CollectionChanged); // Select the first document if any. Document = Documents.FirstOrDefault(); }