using BPASmartClient.Compiler; using BPASmartClient.DATABUS; using BPASmartClient.MessageName.EnumHelp; using BPASmartClient.MessageName.接收消息Model.物料仓; using BPASmartClient.SCADAControl.Converters; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; 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 { /// /// TheDataGrid.xaml 的交互逻辑 /// public partial class TheDataGrid :DataGrid, IExecutable { public TheDataGrid() { InitializeComponent(); ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); MinWidth = 100; MinHeight = 100; } public DataSouceModel ItemsString { get { return (DataSouceModel)GetValue(ItemsStringProperty); } set { SetValue(ItemsStringProperty,value); } } public static readonly DependencyProperty ItemsStringProperty = DependencyProperty.Register("ItemsString",typeof(DataSouceModel),typeof(TheDataGrid),new PropertyMetadata(new DataSouceModel())); public string ControlType => "控件"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { //Style = null; Register(); } } } DispatcherTimer timer = new DispatcherTimer(); /// /// 注册需要处理的事件 /// public void Register() { timer.Interval = TimeSpan.FromMilliseconds(TimeCount); timer.Tick += Timer_Tick; ; timer.Start(); } private void Timer_Tick(object? sender,EventArgs e) { if (!string.IsNullOrEmpty(DataSouceInformation)) { if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation)) { FDataSouce= Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation];// = GenerateData; } } } #region 数据绑定模块 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(5)); [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(string.Empty)); [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 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 { ItemsString = JsonConvert.DeserializeObject(GenerateData); // 运行时进行项目绑定 Binding binding = new Binding(); binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; binding.Path = new PropertyPath("ItemsString.data"); SetBinding(ItemsSourceProperty,binding); } catch (Exception ex) { } } } #endregion } public class DataSouceModel { public List data {get; set; } public DataSouceModel() { data = new List(); } } }