终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

337 wiersze
13 KiB

  1. using BPASmartClient.Compiler;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Threading;
  12. namespace BPASmartClient.SCADAControl.CustomerControls
  13. {
  14. /// <summary>
  15. /// TheDataGrid.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class TheDataGrid :DataGrid, IExecutable, IDisposable
  18. {
  19. public TheDataGrid()
  20. {
  21. InitializeComponent();
  22. Width = 100;
  23. Height = 100;
  24. this.Loaded += TheDataGrid_Loaded; ;
  25. }
  26. private void TheDataGrid_Loaded(object sender, RoutedEventArgs e)
  27. {
  28. TabItems.CollectionChanged += TabItems_CollectionChanged;
  29. }
  30. private void TabItems_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  31. {
  32. if (TabItems.Count > 0)
  33. {
  34. try
  35. {
  36. TabItemsStr = JsonConvert.SerializeObject(TabItems);
  37. GenerateDataRefresh();
  38. }
  39. catch (Exception ex)
  40. {
  41. }
  42. }
  43. }
  44. public string ControlType => "控件";
  45. private bool isExecuteState;
  46. public bool IsExecuteState
  47. {
  48. get { return isExecuteState; }
  49. set
  50. {
  51. isExecuteState = value;
  52. if (IsExecuteState)
  53. {
  54. //Style = null;
  55. Register();
  56. }
  57. }
  58. }
  59. DispatcherTimer timer = new DispatcherTimer();
  60. /// <summary>
  61. /// 注册需要处理的事件
  62. /// </summary>
  63. public void Register()
  64. {
  65. if (!string.IsNullOrEmpty(TabItemsStr))
  66. {
  67. try
  68. {
  69. TabItems = JsonConvert.DeserializeObject<ObservableCollection<列显示设置>>(TabItemsStr);
  70. }
  71. catch (Exception ex)
  72. {
  73. }
  74. }
  75. Style = null;
  76. if (DataSouceType == DataTypeEnum.API接口)
  77. {
  78. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  79. timer.Tick += Timer_Tick; ;
  80. timer.Start();
  81. }
  82. }
  83. public void Dispose()
  84. {
  85. timer.Stop();
  86. FDataSouce = "";
  87. if (IsRun) IsRun = false;
  88. GenerateDataRefresh();
  89. }
  90. private void Timer_Tick(object? sender,EventArgs e)
  91. {
  92. if (!string.IsNullOrEmpty(DataSouceInformation))
  93. {
  94. if (DataSouceType == DataTypeEnum.API接口)
  95. {
  96. if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation))
  97. {
  98. FDataSouce = Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation];// = GenerateData;
  99. }
  100. }
  101. else if (DataSouceType == DataTypeEnum.Redis)
  102. {
  103. if (DataSouceInformation.Contains(".") && DataSouceInformation.Contains("{Binding "))
  104. {
  105. string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split(".");
  106. if (str.Length > 1)
  107. {
  108. if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0]))
  109. {
  110. Dictionary<string, object> b = Class_DataBus.GetInstance().Dic_RedisData[str[0]];
  111. if (b != null && b.ContainsKey(str[1]))
  112. FDataSouce = b[str[1]].ToString();
  113. }
  114. }
  115. }
  116. }
  117. else if (DataSouceType == DataTypeEnum.静态数据)
  118. {
  119. FDataSouce = "";
  120. }
  121. }
  122. }
  123. #region 数据绑定模块
  124. [Category("名称[自动生成]")]
  125. private string TabItemsStr
  126. {
  127. get { return (string)GetValue(TabItemsStrProperty); }
  128. set { SetValue(TabItemsStrProperty, value); }
  129. }
  130. private static readonly DependencyProperty TabItemsStrProperty =
  131. DependencyProperty.Register("TabItemsStr", typeof(string), typeof(TheDataGrid), new PropertyMetadata(string.Empty));
  132. [Category("集合")]
  133. public ObservableCollection<列显示设置> TabItems
  134. {
  135. get { return (ObservableCollection<列显示设置>)GetValue(TabItemsProperty); }
  136. set { SetValue(TabItemsProperty, value); }
  137. }
  138. private static readonly DependencyProperty TabItemsProperty =
  139. DependencyProperty.Register("TabItems", typeof(ObservableCollection<列显示设置>), typeof(TheDataGrid), new PropertyMetadata(new ObservableCollection<列显示设置>()));
  140. public event EventHandler PropertyChange; //声明一个事件
  141. [Category("数据绑定-数据来源")]
  142. public DataTypeEnum DataSouceType
  143. {
  144. get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); }
  145. set { SetValue(DataSouceTypeProperty,value); }
  146. }
  147. public static readonly DependencyProperty DataSouceTypeProperty =
  148. DependencyProperty.Register("DataSouceType",typeof(DataTypeEnum),typeof(TheDataGrid),new PropertyMetadata(DataTypeEnum.API接口));
  149. [Category("数据绑定-数据来源")]
  150. public int TimeCount
  151. {
  152. get { return (int)GetValue(TimeCountProperty); }
  153. set { SetValue(TimeCountProperty,value); }
  154. }
  155. public static readonly DependencyProperty TimeCountProperty =
  156. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(500));
  157. [Category("数据绑定-数据来源")]
  158. public string DataSouceInformation
  159. {
  160. get { return (string)GetValue(DataSouceInformationProperty); }
  161. set { SetValue(DataSouceInformationProperty,value); }
  162. }
  163. public static readonly DependencyProperty DataSouceInformationProperty =
  164. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata("api0"));
  165. [Category("数据绑定")]
  166. public string FDataSouce
  167. {
  168. get { return (string)GetValue(FDataSouceProperty); }
  169. set { SetValue(FDataSouceProperty,value); }
  170. }
  171. public static readonly DependencyProperty FDataSouceProperty =
  172. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  173. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.DataSouceRefresh();
  174. public void DataSouceRefresh()
  175. {
  176. try
  177. {
  178. //if (!string.IsNullOrEmpty(FDataSouce))
  179. {
  180. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  181. if (PropertyChange != null)
  182. {
  183. PropertyChange(this,null);
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. }
  190. }
  191. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  192. [Category("数据绑定")]
  193. public string Code
  194. {
  195. get { return (string)GetValue(CodeProperty); }
  196. set { SetValue(CodeProperty,value); }
  197. }
  198. public static readonly DependencyProperty CodeProperty =
  199. DependencyProperty.Register("Code",typeof(string),typeof(TheDataGrid),new PropertyMetadata(_code));
  200. [Category("数据绑定")]
  201. public bool IsRun
  202. {
  203. get { return (bool)GetValue(RunProperty); }
  204. set { SetValue(RunProperty, value); }
  205. }
  206. public static readonly DependencyProperty RunProperty =
  207. DependencyProperty.Register("IsRun", typeof(bool), typeof(TheDataGrid), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
  208. private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.IsRunRefresh();
  209. public void IsRunRefresh()
  210. {
  211. //测试运行
  212. if (IsRun)
  213. IsExecuteState = true;
  214. else
  215. {
  216. IsExecuteState = false;
  217. Dispose();
  218. }
  219. }
  220. [Category("数据绑定")]
  221. public string GenerateData
  222. {
  223. get { return (string)GetValue(GenerateDataProperty); }
  224. set { SetValue(GenerateDataProperty,value); }
  225. }
  226. public static readonly DependencyProperty GenerateDataProperty =
  227. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onGenerateDataChanged)));
  228. private static void onGenerateDataChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.GenerateDataRefresh();
  229. public void GenerateDataRefresh()
  230. {
  231. if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data"))
  232. {
  233. try
  234. {
  235. Dictionary<string, object> keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData);
  236. List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(keys["data"].ToString());
  237. //ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData);
  238. // 运行时进行项目绑定
  239. //this.AutoGenerateColumns = false;
  240. this.CanUserAddRows = false;
  241. this.CanUserDeleteRows = false;
  242. this.RowHeaderWidth = 0;
  243. this.GridLinesVisibility = DataGridGridLinesVisibility.None;
  244. this.BorderThickness = new Thickness(0);
  245. this.ItemsSource = ListToDataTable(obj2).DefaultView;
  246. }
  247. catch (Exception ex)
  248. {
  249. }
  250. }
  251. else
  252. {
  253. // this.ItemsSource = new DataTable().DefaultView;
  254. }
  255. }
  256. public DataTable ListToDataTable(List<Dictionary<string, object>> arrayList)
  257. {
  258. DataTable dataTable = new DataTable(); //实例化
  259. DataTable result;
  260. try
  261. {
  262. if (arrayList.Count > 0)
  263. {
  264. foreach (Dictionary<string, object> dictionary in arrayList)
  265. {
  266. if (dictionary.Keys.Count<string>() == 0)
  267. {
  268. result = dataTable;
  269. return result;
  270. }
  271. if (TabItems == null || TabItems.Count == 0)
  272. {
  273. foreach (string current in dictionary.Keys)
  274. {
  275. TabItems.Add(new 列显示设置 { 列标题 = current, 是否显示 = true, 显示名称 = current });
  276. }
  277. }
  278. //在第一次循环时确定datatable的列名,后续循环无需再更改
  279. if (dataTable.Columns.Count == 0)
  280. {
  281. //此处仅考虑一层json字符串的形式,多层结构需要深入得到叶子节点的key
  282. foreach (var current in TabItems)
  283. {
  284. if(current.是否显示)
  285. dataTable.Columns.Add(current.显示名称);//, dictionary[current].GetType());
  286. }
  287. }
  288. //每次循环增加datatable的Rows
  289. DataRow dataRow = dataTable.NewRow();
  290. foreach (var current in TabItems)
  291. {
  292. if (current.是否显示)
  293. dataRow[current.显示名称] = dictionary[current.列标题];
  294. }
  295. dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
  296. }
  297. }
  298. }
  299. catch
  300. {
  301. }
  302. result = dataTable;
  303. return result;
  304. }
  305. #endregion
  306. }
  307. public class 列显示设置
  308. {
  309. public string 列标题 { get; set; }
  310. public string 显示名称 { get; set; }
  311. public bool 是否显示 { get; set; }
  312. }
  313. }