BackendSCConfigView.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.UI.MVVM;
  11. namespace MECF.Framework.RT.Core.Backend
  12. {
  13. public class NotifiableSCConfigItem : SCConfigItem, INotifyPropertyChanged
  14. {
  15. public string SetPoint { get; set; }
  16. public int Index { get; set; }
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. public void InvokePropertyChanged(string propertyName)
  19. {
  20. if (PropertyChanged != null)
  21. {
  22. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  23. }
  24. }
  25. public void InvokePropertyChanged()
  26. {
  27. PropertyInfo[] ps = this.GetType().GetProperties();
  28. foreach (PropertyInfo p in ps)
  29. {
  30. InvokePropertyChanged(p.Name);
  31. if (p.PropertyType == typeof(ICommand))
  32. {
  33. DelegateCommand<string> cmd = p.GetValue(this, null) as DelegateCommand<string>;
  34. if (cmd != null)
  35. cmd.RaiseCanExecuteChanged();
  36. }
  37. }
  38. FieldInfo[] fi = this.GetType().GetFields();
  39. foreach (FieldInfo p in fi)
  40. {
  41. InvokePropertyChanged(p.Name);
  42. if (p.FieldType == typeof(ICommand))
  43. {
  44. DelegateCommand<string> cmd = p.GetValue(this) as DelegateCommand<string>;
  45. if (cmd != null)
  46. cmd.RaiseCanExecuteChanged();
  47. }
  48. }
  49. }
  50. }
  51. public class BackendSCConfigViewModel : ViewModelBase
  52. {
  53. private List<SCConfigItem> _scItems;
  54. private ObservableCollection<NotifiableSCConfigItem> _ScItemList;
  55. public ObservableCollection<NotifiableSCConfigItem> ScItemList
  56. {
  57. get { return _ScItemList; }
  58. set { _ScItemList = value;this.InvokePropertyChanged(nameof(ScItemList)); }
  59. }
  60. public string TextFilter { get; set; }
  61. public ICommand SetScCommand { get; set; }
  62. public ICommand ReloadCommand { get; set; }
  63. public BackendSCConfigViewModel()
  64. {
  65. Init();
  66. SetScCommand = new DelegateCommand<string>(SetSc);
  67. ReloadCommand = new DelegateCommand<string>(Reload);
  68. }
  69. private void SetSc(string obj)
  70. {
  71. NotifiableSCConfigItem item = ScItemList.First(x => x.PathName == obj);
  72. if (item.Type != SCConfigType.String.ToString() && string.IsNullOrEmpty(item.SetPoint))
  73. return;
  74. SC.SetItemValueFromString(obj, item.SetPoint == null ? "" : item.SetPoint);
  75. item.BoolValue = SC.GetConfigItem(obj).BoolValue;
  76. item.IntValue = SC.GetConfigItem(obj).IntValue;
  77. item.StringValue = SC.GetConfigItem(obj).StringValue;
  78. item.DoubleValue = SC.GetConfigItem(obj).DoubleValue;
  79. item.InvokePropertyChanged(nameof(item.Value));
  80. }
  81. public void Reload(string obj)
  82. {
  83. Init(TextFilter);
  84. foreach (var item in ScItemList)
  85. {
  86. item.BoolValue = SC.GetConfigItem(item.PathName).BoolValue;
  87. item.IntValue = SC.GetConfigItem(item.PathName).IntValue;
  88. item.StringValue = SC.GetConfigItem(item.PathName).StringValue;
  89. item.DoubleValue = SC.GetConfigItem(item.PathName).DoubleValue;
  90. item.InvokePropertyChanged(nameof(item.Value));
  91. }
  92. }
  93. void Init(string strFilter="")
  94. {
  95. _scItems = SC.GetItemList();
  96. ScItemList = new ObservableCollection<NotifiableSCConfigItem>();
  97. int _index = 0;
  98. foreach (var scItem in _scItems.Where(x => string.IsNullOrEmpty(strFilter) || x.PathName.ToLower().Contains(strFilter.ToLower())))
  99. {
  100. _index++;
  101. ScItemList.Add(new NotifiableSCConfigItem()
  102. {
  103. Index = _index,
  104. BoolValue = scItem.BoolValue,
  105. Default = scItem.Default,
  106. Description = scItem.Description,
  107. DoubleValue = scItem.DoubleValue,
  108. IntValue = scItem.IntValue,
  109. Max = scItem.Max,
  110. Min = scItem.Min,
  111. Name = scItem.Name,
  112. Type = scItem.Type,
  113. Tag = scItem.Tag,
  114. Parameter = scItem.Parameter,
  115. Path = scItem.Path,
  116. SetPoint = scItem.Value.ToString(),
  117. StringValue = scItem.StringValue,
  118. });
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// SCConfigView.xaml 的交互逻辑
  124. /// </summary>
  125. public partial class BackendSCConfigView : UserControl
  126. {
  127. public BackendSCConfigView()
  128. {
  129. InitializeComponent();
  130. this.Loaded += SCConfigView_Loaded;
  131. this.IsVisibleChanged += BackendSCConfigView_IsVisibleChanged;
  132. }
  133. private void BackendSCConfigView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  134. {
  135. if (this.DataContext == null)
  136. {
  137. DataContext = new BackendSCConfigViewModel();
  138. }
  139. (this.DataContext as BackendSCConfigViewModel).Reload(null);
  140. }
  141. private void SCConfigView_Loaded(object sender, RoutedEventArgs e)
  142. {
  143. if (this.DataContext == null)
  144. {
  145. DataContext = new BackendSCConfigViewModel();
  146. }
  147. }
  148. }
  149. }