BackendSCConfigView.xaml.cs 5.6 KB

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