终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

199 lines
7.4 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.DATABUS;
  3. using BPASmartClient.MessageName.EnumHelp;
  4. using BPASmartClient.MessageName.接收消息Model.物料仓;
  5. using BPASmartClient.SCADAControl.Converters;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using System.Windows.Threading;
  24. namespace BPASmartClient.SCADAControl.CustomerControls
  25. {
  26. /// <summary>
  27. /// TheDataGrid.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheDataGrid :DataGrid, IExecutable
  30. {
  31. public TheDataGrid()
  32. {
  33. InitializeComponent();
  34. ResourceDictionary languageResDic = new ResourceDictionary();
  35. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
  36. this.Resources.MergedDictionaries.Add(languageResDic);
  37. MinWidth = 100;
  38. MinHeight = 100;
  39. }
  40. public DataSouceModel ItemsString
  41. {
  42. get { return (DataSouceModel)GetValue(ItemsStringProperty); }
  43. set { SetValue(ItemsStringProperty,value); }
  44. }
  45. public static readonly DependencyProperty ItemsStringProperty =
  46. DependencyProperty.Register("ItemsString",typeof(DataSouceModel),typeof(TheDataGrid),new PropertyMetadata(new DataSouceModel()));
  47. public string ControlType => "控件";
  48. private bool isExecuteState;
  49. public bool IsExecuteState
  50. {
  51. get { return isExecuteState; }
  52. set
  53. {
  54. isExecuteState = value;
  55. if (IsExecuteState)
  56. {
  57. //Style = null;
  58. Register();
  59. }
  60. }
  61. }
  62. DispatcherTimer timer = new DispatcherTimer();
  63. /// <summary>
  64. /// 注册需要处理的事件
  65. /// </summary>
  66. public void Register()
  67. {
  68. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  69. timer.Tick += Timer_Tick; ;
  70. timer.Start();
  71. }
  72. private void Timer_Tick(object? sender,EventArgs e)
  73. {
  74. if (!string.IsNullOrEmpty(DataSouceInformation))
  75. {
  76. if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation))
  77. {
  78. FDataSouce= Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation];// = GenerateData;
  79. }
  80. }
  81. }
  82. #region 数据绑定模块
  83. public event EventHandler PropertyChange; //声明一个事件
  84. [Category("数据绑定-数据来源")]
  85. public DataTypeEnum DataSouceType
  86. {
  87. get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); }
  88. set { SetValue(DataSouceTypeProperty,value); }
  89. }
  90. public static readonly DependencyProperty DataSouceTypeProperty =
  91. DependencyProperty.Register("DataSouceType",typeof(DataTypeEnum),typeof(TheDataGrid),new PropertyMetadata(DataTypeEnum.API接口));
  92. [Category("数据绑定-数据来源")]
  93. public int TimeCount
  94. {
  95. get { return (int)GetValue(TimeCountProperty); }
  96. set { SetValue(TimeCountProperty,value); }
  97. }
  98. public static readonly DependencyProperty TimeCountProperty =
  99. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(5));
  100. [Category("数据绑定-数据来源")]
  101. public string DataSouceInformation
  102. {
  103. get { return (string)GetValue(DataSouceInformationProperty); }
  104. set { SetValue(DataSouceInformationProperty,value); }
  105. }
  106. public static readonly DependencyProperty DataSouceInformationProperty =
  107. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty));
  108. [Category("数据绑定")]
  109. public string FDataSouce
  110. {
  111. get { return (string)GetValue(FDataSouceProperty); }
  112. set { SetValue(FDataSouceProperty,value); }
  113. }
  114. public static readonly DependencyProperty FDataSouceProperty =
  115. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  116. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.DataSouceRefresh();
  117. public void DataSouceRefresh()
  118. {
  119. try
  120. {
  121. if (!string.IsNullOrEmpty(FDataSouce))
  122. {
  123. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  124. if (PropertyChange != null)
  125. {
  126. PropertyChange(this,null);
  127. }
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. }
  133. }
  134. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  135. [Category("数据绑定")]
  136. public string Code
  137. {
  138. get { return (string)GetValue(CodeProperty); }
  139. set { SetValue(CodeProperty,value); }
  140. }
  141. public static readonly DependencyProperty CodeProperty =
  142. DependencyProperty.Register("Code",typeof(string),typeof(TheDataGrid),new PropertyMetadata(_code));
  143. [Category("数据绑定")]
  144. public string GenerateData
  145. {
  146. get { return (string)GetValue(GenerateDataProperty); }
  147. set { SetValue(GenerateDataProperty,value); }
  148. }
  149. public static readonly DependencyProperty GenerateDataProperty =
  150. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onGenerateDataChanged)));
  151. private static void onGenerateDataChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.GenerateDataRefresh();
  152. public void GenerateDataRefresh()
  153. {
  154. if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data"))
  155. {
  156. try
  157. {
  158. ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData);
  159. // 运行时进行项目绑定
  160. Binding binding = new Binding();
  161. binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self };
  162. binding.Path = new PropertyPath("ItemsString.data");
  163. SetBinding(ItemsSourceProperty,binding);
  164. }
  165. catch (Exception ex)
  166. {
  167. }
  168. }
  169. }
  170. #endregion
  171. }
  172. public class DataSouceModel
  173. {
  174. public List<object> data {get; set; }
  175. public DataSouceModel()
  176. {
  177. data = new List<object>();
  178. }
  179. }
  180. }