From b553414ee6ff10744713ced92bfac3b8987d9b75 Mon Sep 17 00:00:00 2001 From: fyf Date: Thu, 22 Sep 2022 14:38:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.DATABUS/Class_DataBus.cs | 10 +- .../CustomerControls/TheAPI.xaml.cs | 14 ++- .../CustomerControls/TheDataGrid.xaml.cs | 115 +++++++++++++----- .../Themes/Generic.xaml | 3 +- .../Controls/MainCanvasPanel.xaml.cs | 6 +- BeDesignerSCADA/ViewModel/DataBusModel.cs | 84 +++++++++++++ BeDesignerSCADA/ViewModel/MainViewModelNew.cs | 24 ++++ 7 files changed, 216 insertions(+), 40 deletions(-) create mode 100644 BeDesignerSCADA/ViewModel/DataBusModel.cs diff --git a/BPASmartClient.DATABUS/Class_DataBus.cs b/BPASmartClient.DATABUS/Class_DataBus.cs index cd3aee39..6261db7d 100644 --- a/BPASmartClient.DATABUS/Class_DataBus.cs +++ b/BPASmartClient.DATABUS/Class_DataBus.cs @@ -32,7 +32,14 @@ namespace BPASmartClient.DATABUS /// /// API数据 /// - public ConcurrentDictionary Dic_APIData = new ConcurrentDictionary(); + public ConcurrentDictionary Dic_APIData = new ConcurrentDictionary(); + #endregion + + #region 配置数据 + + + + //Json.Read(); #endregion } @@ -42,7 +49,6 @@ namespace BPASmartClient.DATABUS public string VarName { get; set; } public string VarVaule { get; set; } public string DataType { get; set; } - } } \ No newline at end of file diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs index 487e5127..03606fab 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheAPI.xaml.cs @@ -88,7 +88,6 @@ namespace BPASmartClient.SCADAControl.CustomerControls if (IsExecuteState) { Register(); - //Style = null; } } } @@ -301,6 +300,19 @@ namespace BPASmartClient.SCADAControl.CustomerControls public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code",typeof(string),typeof(TheAPI),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(TheAPI), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged))); + private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.IsRunRefresh(); + public void IsRunRefresh() + { + //测试运行 + } + [Category("数据绑定")] public string GenerateData { get { return (string)GetValue(GenerateDataProperty); } diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs index 6e90ae52..d61febe5 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs @@ -33,23 +33,34 @@ namespace BPASmartClient.SCADAControl.CustomerControls 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; - //Width = 100; - //Height = 100; + //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; + Width = 100; + Height = 100; + this.Loaded += TheDataGrid_Loaded; ; } - public DataSouceModel ItemsString + private void TheDataGrid_Loaded(object sender, RoutedEventArgs e) { - get { return (DataSouceModel)GetValue(ItemsStringProperty); } - set { SetValue(ItemsStringProperty,value); } + TabItems.CollectionChanged += TabItems_CollectionChanged; } - public static readonly DependencyProperty ItemsStringProperty = - DependencyProperty.Register("ItemsString",typeof(DataSouceModel),typeof(TheDataGrid),new PropertyMetadata(new DataSouceModel())); + private void TabItems_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + if (TabItems.Count > 0) + { + try + { + TabItemsStr = JsonConvert.SerializeObject(TabItems); + } + catch (Exception ex) + { + } + } + } public string ControlType => "控件"; @@ -67,12 +78,24 @@ namespace BPASmartClient.SCADAControl.CustomerControls } } } + DispatcherTimer timer = new DispatcherTimer(); /// /// 注册需要处理的事件 /// public void Register() { + if (!string.IsNullOrEmpty(TabItemsStr)) + { + try + { + TabItems = JsonConvert.DeserializeObject>(TabItemsStr); + } + catch (Exception ex) + { + + } + } if (DataSouceType == DataTypeEnum.API接口) { @@ -95,6 +118,22 @@ namespace BPASmartClient.SCADAControl.CustomerControls #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 @@ -119,9 +158,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls set { SetValue(DataSouceInformationProperty,value); } } public static readonly DependencyProperty DataSouceInformationProperty = - DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty)); - - + DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata("api0")); [Category("数据绑定")] public string FDataSouce { @@ -173,14 +210,17 @@ namespace BPASmartClient.SCADAControl.CustomerControls { try { - //Dictionary keys= JsonConvert.DeserializeObject>(GenerateData); - //List> obj2 = JsonConvert.DeserializeObject>>(keys["data"].ToString()); - ItemsString = JsonConvert.DeserializeObject(GenerateData); + Dictionary keys= JsonConvert.DeserializeObject>(GenerateData); + List> obj2 = JsonConvert.DeserializeObject>>(keys["data"].ToString()); + //ItemsString = JsonConvert.DeserializeObject(GenerateData); // 运行时进行项目绑定 - Binding binding = new Binding(); - binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; - binding.Path = new PropertyPath("ItemsString.data"); - SetBinding(ItemsSourceProperty,binding); + //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) { @@ -198,6 +238,8 @@ namespace BPASmartClient.SCADAControl.CustomerControls { if (arrayList.Count > 0) { + + foreach (Dictionary dictionary in arrayList) { if (dictionary.Keys.Count() == 0) @@ -205,20 +247,31 @@ namespace BPASmartClient.SCADAControl.CustomerControls 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 (string current in dictionary.Keys) + foreach (var current in TabItems) { - dataTable.Columns.Add(current);//, dictionary[current].GetType()); + if(current.是否显示) + dataTable.Columns.Add(current.显示名称);//, dictionary[current].GetType()); } } //每次循环增加datatable的Rows DataRow dataRow = dataTable.NewRow(); - foreach (string current in dictionary.Keys) + foreach (var current in TabItems) { - dataRow[current] = dictionary[current]; + if (current.是否显示) + dataRow[current.显示名称] = dictionary[current.列标题]; } dataTable.Rows.Add(dataRow); //循环添加行到DataTable中 } @@ -235,12 +288,10 @@ namespace BPASmartClient.SCADAControl.CustomerControls } - public class DataSouceModel + public class 列显示设置 { - public List data {get; set; } - public DataSouceModel() - { - data = new List(); - } + public string 列标题 { get; set; } + public string 显示名称 { get; set; } + public bool 是否显示 { get; set; } } } diff --git a/BPASmartClient.SCADAControl/Themes/Generic.xaml b/BPASmartClient.SCADAControl/Themes/Generic.xaml index f0ebfe2c..05e3ef2d 100644 --- a/BPASmartClient.SCADAControl/Themes/Generic.xaml +++ b/BPASmartClient.SCADAControl/Themes/Generic.xaml @@ -1137,10 +1137,11 @@