using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Text; using System.Windows.Input; using RGBController2.Commands; using RGBController2.ViewModels.Tabs; namespace RGBController2.ViewModels { class MainViewModel { private readonly ObservableCollection _tabs; public ICommand NewTabCommand { get; } public ICollection Tabs { get; } public MainViewModel() { NewTabCommand = new ActionCommand(p => NewTab()); _tabs = new ObservableCollection(); _tabs.CollectionChanged += Tabs_CollectionChanged; Tabs = _tabs; } private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { ITab tab; switch (e.Action) { case NotifyCollectionChangedAction.Add: tab = (ITab) e.NewItems[0]; tab.CloseRequested += OnTabCloseRequested; break; case NotifyCollectionChangedAction.Remove: tab = (ITab) e.OldItems[0]; tab.CloseRequested -= OnTabCloseRequested; break; } } private void OnTabCloseRequested(object sender, EventArgs e) { Tabs.Remove((ITab) sender); } private void NewTab() { Tabs.Add(new ArduinoTab(DateTime.UtcNow.ToString())); } } }