|
- using BPASmartClient.Compiler;
- using BPASmartClient.DATABUS;
- using BPASmartClient.SCADAControl.Converters;
- using BPASmartClient.SCADAControl.EnumClass;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
-
- namespace BPASmartClient.SCADAControl.CustomerControls
- {
- /// <summary>
- /// TheDataGrid.xaml 的交互逻辑
- /// </summary>
- public partial class TheDataGrid :DataGrid, IExecutable, IDisposable
- {
- public TheDataGrid()
- {
- InitializeComponent();
- Width = 100;
- Height = 100;
- this.Loaded += TheDataGrid_Loaded; ;
- }
-
- private void TheDataGrid_Loaded(object sender, RoutedEventArgs e)
- {
- TabItems.CollectionChanged += TabItems_CollectionChanged;
- }
-
- private void TabItems_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- if (TabItems.Count > 0)
- {
- try
- {
- TabItemsStr = JsonConvert.SerializeObject(TabItems);
- GenerateDataRefresh();
- }
- catch (Exception ex)
- {
- }
- }
- }
-
- public string ControlType => "控件";
-
- private bool isExecuteState;
- public bool IsExecuteState
- {
- get { return isExecuteState; }
- set
- {
- isExecuteState = value;
- if (IsExecuteState)
- {
- //Style = null;
- Register();
- }
- }
- }
-
- DispatcherTimer timer = new DispatcherTimer();
- /// <summary>
- /// 注册需要处理的事件
- /// </summary>
- public void Register()
- {
- if (!string.IsNullOrEmpty(TabItemsStr))
- {
- try
- {
- TabItems = JsonConvert.DeserializeObject<ObservableCollection<列显示设置>>(TabItemsStr);
- }
- catch (Exception ex)
- {
-
- }
- }
- Style = null;
-
- if (DataSouceType == DataTypeEnum.API接口)
- {
- timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
- timer.Tick += Timer_Tick; ;
- timer.Start();
- }
- }
-
- public void Dispose()
- {
- timer.Stop();
- FDataSouce = "";
- if (IsRun) IsRun = false;
- GenerateDataRefresh();
- }
-
- private void Timer_Tick(object? sender,EventArgs e)
- {
- if (!string.IsNullOrEmpty(DataSouceInformation))
- {
- if (DataSouceType == DataTypeEnum.API接口)
- {
- if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation))
- {
- FDataSouce = Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation];// = GenerateData;
- }
- }
- else if (DataSouceType == DataTypeEnum.Redis)
- {
- if (DataSouceInformation.Contains(".") && DataSouceInformation.Contains("{Binding "))
- {
- string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split(".");
- if (str.Length > 1)
- {
- if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0]))
- {
- Dictionary<string, object> b = Class_DataBus.GetInstance().Dic_RedisData[str[0]];
- if (b != null && b.ContainsKey(str[1]))
- FDataSouce = b[str[1]].ToString();
- }
- }
- }
- }
- else if (DataSouceType == DataTypeEnum.静态数据)
- {
- FDataSouce = "";
- }
- }
- }
-
-
- #region 数据绑定模块
- [Category("名称[自动生成]")]
- private string TabItemsStr
- {
- get { return (string)GetValue(TabItemsStrProperty); }
- set { SetValue(TabItemsStrProperty, value); }
- }
- private static readonly DependencyProperty TabItemsStrProperty =
- DependencyProperty.Register("TabItemsStr", typeof(string), typeof(TheDataGrid), new PropertyMetadata(string.Empty));
- [Category("集合")]
- public ObservableCollection<列显示设置> TabItems
- {
- get { return (ObservableCollection<列显示设置>)GetValue(TabItemsProperty); }
- set { SetValue(TabItemsProperty, value); }
- }
- private static readonly DependencyProperty TabItemsProperty =
- DependencyProperty.Register("TabItems", typeof(ObservableCollection<列显示设置>), typeof(TheDataGrid), new PropertyMetadata(new ObservableCollection<列显示设置>()));
- public event EventHandler PropertyChange; //声明一个事件
- [Category("数据绑定-数据来源")]
- public DataTypeEnum DataSouceType
- {
- get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); }
- set { SetValue(DataSouceTypeProperty,value); }
- }
- public static readonly DependencyProperty DataSouceTypeProperty =
- DependencyProperty.Register("DataSouceType",typeof(DataTypeEnum),typeof(TheDataGrid),new PropertyMetadata(DataTypeEnum.API接口));
- [Category("数据绑定-数据来源")]
- public int TimeCount
- {
- get { return (int)GetValue(TimeCountProperty); }
- set { SetValue(TimeCountProperty,value); }
- }
- public static readonly DependencyProperty TimeCountProperty =
- DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(500));
- [Category("数据绑定-数据来源")]
- public string DataSouceInformation
- {
- get { return (string)GetValue(DataSouceInformationProperty); }
- set { SetValue(DataSouceInformationProperty,value); }
- }
- public static readonly DependencyProperty DataSouceInformationProperty =
- DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata("api0"));
- [Category("数据绑定")]
- public string FDataSouce
- {
- get { return (string)GetValue(FDataSouceProperty); }
- set { SetValue(FDataSouceProperty,value); }
- }
- public static readonly DependencyProperty FDataSouceProperty =
- DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
- private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.DataSouceRefresh();
- public void DataSouceRefresh()
- {
- try
- {
- //if (!string.IsNullOrEmpty(FDataSouce))
- {
- GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
- if (PropertyChange != null)
- {
- PropertyChange(this,null);
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
- [Category("数据绑定")]
- public string Code
- {
- get { return (string)GetValue(CodeProperty); }
- set { SetValue(CodeProperty,value); }
- }
- public static readonly DependencyProperty CodeProperty =
- DependencyProperty.Register("Code",typeof(string),typeof(TheDataGrid),new PropertyMetadata(_code));
- [Category("数据绑定")]
- public bool IsRun
- {
- get { return (bool)GetValue(RunProperty); }
- set { SetValue(RunProperty, value); }
- }
- public static readonly DependencyProperty RunProperty =
- DependencyProperty.Register("IsRun", typeof(bool), typeof(TheDataGrid), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
- private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.IsRunRefresh();
- public void IsRunRefresh()
- {
- //测试运行
- if (IsRun)
- IsExecuteState = true;
- else
- {
- IsExecuteState = false;
- Dispose();
- }
- }
- [Category("数据绑定")]
- public string GenerateData
- {
- get { return (string)GetValue(GenerateDataProperty); }
- set { SetValue(GenerateDataProperty,value); }
- }
- public static readonly DependencyProperty GenerateDataProperty =
- DependencyProperty.Register("GenerateData",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onGenerateDataChanged)));
- private static void onGenerateDataChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.GenerateDataRefresh();
- public void GenerateDataRefresh()
- {
- if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data"))
- {
- try
- {
- Dictionary<string, object> keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData);
- List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(keys["data"].ToString());
- //ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData);
- // 运行时进行项目绑定
- //this.AutoGenerateColumns = false;
- this.CanUserAddRows = false;
- this.CanUserDeleteRows = false;
- this.RowHeaderWidth = 0;
- this.GridLinesVisibility = DataGridGridLinesVisibility.None;
- this.BorderThickness = new Thickness(0);
- this.ItemsSource = ListToDataTable(obj2).DefaultView;
- }
- catch (Exception ex)
- {
-
- }
- }
- else
- {
- // this.ItemsSource = new DataTable().DefaultView;
- }
-
- }
-
- public DataTable ListToDataTable(List<Dictionary<string, object>> arrayList)
- {
- DataTable dataTable = new DataTable(); //实例化
- DataTable result;
- try
- {
- if (arrayList.Count > 0)
- {
-
-
- foreach (Dictionary<string, object> dictionary in arrayList)
- {
- if (dictionary.Keys.Count<string>() == 0)
- {
- result = dataTable;
- return result;
- }
-
- if (TabItems == null || TabItems.Count == 0)
- {
- foreach (string current in dictionary.Keys)
- {
- TabItems.Add(new 列显示设置 { 列标题 = current, 是否显示 = true, 显示名称 = current });
- }
- }
-
- //在第一次循环时确定datatable的列名,后续循环无需再更改
- if (dataTable.Columns.Count == 0)
- {
- //此处仅考虑一层json字符串的形式,多层结构需要深入得到叶子节点的key
- foreach (var current in TabItems)
- {
- if(current.是否显示)
- dataTable.Columns.Add(current.显示名称);//, dictionary[current].GetType());
- }
- }
- //每次循环增加datatable的Rows
- DataRow dataRow = dataTable.NewRow();
- foreach (var current in TabItems)
- {
- if (current.是否显示)
- dataRow[current.显示名称] = dictionary[current.列标题];
- }
- dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
- }
- }
- }
- catch
- {
- }
- result = dataTable;
- return result;
- }
- #endregion
-
-
- }
-
- public class 列显示设置
- {
- public string 列标题 { get; set; }
- public string 显示名称 { get; set; }
- public bool 是否显示 { get; set; }
- }
- }
|