using System.ComponentModel; using TerminalUI.Controls; namespace TerminalUI; public class DataContextMapper : IDisposable { private readonly Func _mapper; public IView Source { get; } public IView Target { get; } public DataContextMapper(IView source, IView target, Func mapper) { _mapper = mapper; ArgumentNullException.ThrowIfNull(source); Source = source; Target = target; source.PropertyChanged += SourceOnPropertyChanged; } private void SourceOnPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName != nameof(IView.DataContext)) return; Target.DataContext = _mapper(Source.DataContext); } public void Dispose() { Source.PropertyChanged -= SourceOnPropertyChanged; Source.RemoveDisposable(this); Target.RemoveDisposable(this); } }