RopeViewBaseModel.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using SWRIS.Dtos;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Windows;
  5. namespace SWRIS.Models.ViewModel
  6. {
  7. public class RopeViewBaseModel : INotifyPropertyChanged
  8. {
  9. private ObservableCollection<SimpleRecordDto> records;
  10. public ObservableCollection<SimpleRecordDto> Records
  11. {
  12. get => records;
  13. set
  14. {
  15. if (records != value)
  16. {
  17. records = value;
  18. OnPropertyChanged(nameof(Records));
  19. }
  20. }
  21. }
  22. public void AddRecord(SimpleRecordDto record)
  23. {
  24. record.IsNew = true;
  25. SafeCollectionInsert(Records, 0, record);
  26. // 延迟重置 IsNew 状态,避免重复触发动画
  27. var timer = new System.Windows.Threading.DispatcherTimer
  28. {
  29. Interval = System.TimeSpan.FromSeconds(3)
  30. };
  31. timer.Tick += (s, e) =>
  32. {
  33. record.IsNew = false;
  34. timer.Stop();
  35. };
  36. timer.Start();
  37. }
  38. protected void SafeCollectionInsert<T>(ObservableCollection<T> collection, int index, T item)
  39. {
  40. if (Application.Current.Dispatcher.CheckAccess())
  41. {
  42. collection.Insert(index, item);
  43. }
  44. else
  45. {
  46. Application.Current.Dispatcher.Invoke(() => collection.Insert(index, item));
  47. }
  48. }
  49. public event PropertyChangedEventHandler PropertyChanged;
  50. protected internal virtual void OnPropertyChanged(string propertyName)
  51. {
  52. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  53. }
  54. }
  55. }