using TestingModule.ViewModels; using System.Windows.Controls; namespace TestingModule.Views { public partial class LogArea : UserControl { public LogArea() { InitializeComponent(); // 绑定 DataContext 后,订阅日志集合变化 this.Loaded += (s, e) => { if (DataContext is LogAreaViewModel vm) { vm.Logs.CollectionChanged += Logs_CollectionChanged; } }; } private void Logs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // 每次新增日志,滚动到最后一条 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { if (this.LogListView.Items.Count > 0) { this.LogListView.ScrollIntoView(this.LogListView.Items[^1]); } } } } }