using System.Collections.ObjectModel; using System.ComponentModel; namespace TerminalUI.Controls; public abstract class ChildContainerView : View, IChildContainer { private readonly ObservableCollection _children = new(); public ReadOnlyObservableCollection Children { get; } public ChildInitializer ChildInitializer { get; } protected ChildContainerView() { ChildInitializer = new ChildInitializer(this); Children = new ReadOnlyObservableCollection(_children); _children.CollectionChanged += (o, args) => { if (Attached) { if (args.NewItems?.OfType() is { } newItems) { foreach (var newItem in newItems) { newItem.Attached = true; } } ApplicationContext?.EventLoop.RequestRerender(); } }; ((INotifyPropertyChanged)this).PropertyChanged += (o, args) => { if (args.PropertyName == nameof(ApplicationContext)) { foreach (var child in Children) { child.ApplicationContext = ApplicationContext; } } }; } protected override void AttachChildren() { foreach (var child in Children) { child.Attached = true; } } public override TChild AddChild(TChild child) { child = base.AddChild(child); _children.Add(child); return child; } public override TChild AddChild(TChild child, Func dataContextMapper) where TDataContext : default { child = base.AddChild(child, dataContextMapper); _children.Add(child); return child; } }