WarningItemCollection.xaml.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. namespace HistoryView.Controls;
  2. public partial class WarningItemCollection : UserControl
  3. {
  4. public WarningItemCollection()
  5. {
  6. InitializeComponent();
  7. }
  8. public AlarmInfo Latest
  9. {
  10. get { return (AlarmInfo)GetValue(LatestProperty); }
  11. set { SetValue(LatestProperty, value); }
  12. }
  13. public static readonly DependencyProperty LatestProperty =
  14. DependencyProperty.Register("Latest", typeof(AlarmInfo), typeof(WarningItemCollection), new PropertyMetadata(default));
  15. public ObservableDictionary<byte,AlarmInfo> Alarms
  16. {
  17. get { return (ObservableDictionary<byte, AlarmInfo>)GetValue(AlarmsProperty); }
  18. set { SetValue(AlarmsProperty, value); }
  19. }
  20. public static readonly DependencyProperty AlarmsProperty =
  21. DependencyProperty.Register("Alarms", typeof(ObservableDictionary<byte, AlarmInfo>), typeof(WarningItemCollection), new PropertyMetadata(default, PropertyChangedCallback));
  22. private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  23. {
  24. if (d is not WarningItemCollection warningCollection)
  25. return;
  26. if (e.NewValue is not ObservableDictionary<byte, AlarmInfo> and || and is null)
  27. return;
  28. and.CollectionChanged += And_CollectionChanged;
  29. if (and.Any())
  30. warningCollection.Latest = and.LastOrDefault()!.Value;
  31. }
  32. private static void And_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  33. {
  34. if (sender is not WarningItemCollection warningCollection)
  35. return;
  36. if (e.NewItems is not IEnumerable<AlarmInfo> and || and is null)
  37. return;
  38. if (and.Any())
  39. warningCollection.Latest = and.LastOrDefault()!;
  40. }
  41. public ICommand Clear
  42. {
  43. get { return (ICommand)GetValue(ClearProperty); }
  44. set { SetValue(ClearProperty, value); }
  45. }
  46. // Using a DependencyProperty as the backing store for Clear. This enables animation, styling, binding, etc...
  47. public static readonly DependencyProperty ClearProperty =
  48. DependencyProperty.Register("Clear", typeof(ICommand), typeof(WarningItemCollection), new PropertyMetadata(default));
  49. }