终端一体化运控平台
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.
 
 
 

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