From 840d3b130c8c6c6b380e01d95635ba0c8a6ae075 Mon Sep 17 00:00:00 2001 From: fyf Date: Fri, 23 Sep 2022 18:24:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E6=A8=A1=E5=9D=97=20=E5=A2=9E=E5=8A=A0=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E9=9B=86=E5=90=88bing=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerControls/TheDataGrid.xaml.cs | 7 +- .../CustomerControls/TheListBox.xaml.cs | 311 ++++++++++++++++-- BeDesignerSCADA/Controls/MainCanvasPanel.xaml | 26 +- .../Controls/MainCanvasPanel.xaml.cs | 129 +++++++- BeDesignerSCADA/View/ChildEditWindow.xaml | 38 +++ BeDesignerSCADA/View/ChildEditWindow.xaml.cs | 111 +++++++ BeDesignerSCADA/View/JsEditWindow.xaml | 3 +- BeDesignerSCADA/ViewModel/MainViewModelNew.cs | 55 ++-- 8 files changed, 617 insertions(+), 63 deletions(-) create mode 100644 BeDesignerSCADA/View/ChildEditWindow.xaml create mode 100644 BeDesignerSCADA/View/ChildEditWindow.xaml.cs diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs index 8f7d0ed0..c2f27879 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheDataGrid.xaml.cs @@ -33,11 +33,6 @@ 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; this.Loaded += TheDataGrid_Loaded; ; @@ -185,7 +180,7 @@ namespace BPASmartClient.SCADAControl.CustomerControls set { SetValue(TimeCountProperty,value); } } public static readonly DependencyProperty TimeCountProperty = - DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(5)); + DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(500)); [Category("数据绑定-数据来源")] public string DataSouceInformation { diff --git a/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs b/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs index 0742a067..23ce5e72 100644 --- a/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs +++ b/BPASmartClient.SCADAControl/CustomerControls/TheListBox.xaml.cs @@ -1,6 +1,9 @@ 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; @@ -13,17 +16,20 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; +using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using System.Windows.Threading; +using System.Xml; namespace BPASmartClient.SCADAControl.CustomerControls { /// /// TheListBox.xaml 的交互逻辑 /// - public partial class TheListBox :ListBox, IExecutable + public partial class TheListBox :ListBox, IExecutable, IDisposable { public event EventHandler PropertyChange; //声明一个事件 @@ -33,34 +39,155 @@ namespace BPASmartClient.SCADAControl.CustomerControls ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); - //Style = Application.Current.Resources["DesignTheListBox"] as Style; - MinWidth = 100; - MinHeight = 100; - //ItemsString = new ItemsListObj() - //{ - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - // new datalist { Name="23232",Description="wwewewew",Messgae="564654645"}, - //}; + EditorHelper.Register(); + Width = 100; + Height = 100; } - public ItemsListObj ItemsString + #region 属性 + [Category("控件数据集合")] + private ObservableCollection ItemsString { - get { return (ItemsListObj)GetValue(ItemsStringProperty); } - set { SetValue(ItemsStringProperty,value); } + get { return (ObservableCollection)GetValue(ItemsStringProperty); } + set { SetValue(ItemsStringProperty, value); } } + private static readonly DependencyProperty ItemsStringProperty = + DependencyProperty.Register("ItemsString", typeof(ObservableCollection), typeof(TheListBox), new PropertyMetadata(new ObservableCollection())); - public static readonly DependencyProperty ItemsStringProperty = - DependencyProperty.Register("ItemsString",typeof(ItemsListObj),typeof(TheListBox),new PropertyMetadata(null)); + [Category("子控件模板XML格式")] + public string ChildTemplateXml + { + get { return (string)GetValue(ChildTemplateXmlProperty); } + set { SetValue(ChildTemplateXmlProperty, value); } + } + public static readonly DependencyProperty ChildTemplateXmlProperty = + DependencyProperty.Register("ChildTemplateXml", typeof(string), typeof(TheListBox), new PropertyMetadata(string.Empty,new PropertyChangedCallback(onChildTemplateXmlChanged))); + private static void onChildTemplateXmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.XmlRefresh(); + public void XmlRefresh() + { + ; + } + [Category("数据绑定-数据来源")] + public DataTypeEnum DataSouceType + { + get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); } + set { SetValue(DataSouceTypeProperty, value); } + } + public static readonly DependencyProperty DataSouceTypeProperty = + DependencyProperty.Register("DataSouceType", typeof(DataTypeEnum), typeof(TheListBox), 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(TheListBox), 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(TheListBox), 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(TheListBox), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onFDataSouceChanged))); + private static void onFDataSouceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.DataSouceRefresh(); + public void DataSouceRefresh() + { + try + { + 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(TheListBox), 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(TheListBox), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged))); + //private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.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(TheListBox), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onGenerateDataChanged))); + private static void onGenerateDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheListBox)?.GenerateDataRefresh(); + public void GenerateDataRefresh() + { + if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data")) + { + try + { + Dictionary keys = JsonConvert.DeserializeObject>(GenerateData); + ItemsString = JsonConvert.DeserializeObject>(keys["data"].ToString()); + + + //ItemsString = JsonConvert.DeserializeObject(GenerateData); + // 运行时进行项目绑定 + + //this.ItemsSource = ListToDataTable(obj2).DefaultView; + + // 运行时进行项目绑定 + //Binding binding = new Binding(); + //binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; + //binding.Path = new PropertyPath("ItemsString"); + //SetBinding(ItemsSourceProperty,binding); + } + catch (Exception ex) + { + + } + } + else + { + //this.Items.Clear(); + } + + } + #endregion public string ControlType => "控件"; @@ -73,28 +200,162 @@ namespace BPASmartClient.SCADAControl.CustomerControls isExecuteState = value; if (IsExecuteState) { - //Style = null; Register(); } } } + DispatcherTimer timer = new DispatcherTimer(); /// /// 注册需要处理的事件 /// public void Register() { - // 运行时进行项目绑定 + XmlToFrameworkElement(); + + if (DataSouceType == DataTypeEnum.API接口) + { + timer.Interval = TimeSpan.FromMilliseconds(TimeCount); + timer.Tick += Timer_Tick; ; + timer.Start(); + } + } + + /// + /// 流转Xml + /// + /// + public void FrameworkElementToXml(FrameworkElement framework) + { + StringBuilder outstr = new StringBuilder(); + try + { + //this code need for right XML fomating + XmlWriterSettings settings = new XmlWriterSettings + { + Indent = true, + OmitXmlDeclaration = true + }; + var dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings)) + { + //this string need for turning on expression saving mode + XamlWriterMode = XamlWriterMode.Expression + }; + + XamlWriter.Save(framework, dsm); + } + catch (Exception ex) { } + ChildTemplateXml = outstr.ToString(); + } + + public void XmlToFrameworkElement() + { + StackPanel stackPanel = new StackPanel() { Width = 600, Height = 30, Orientation = Orientation.Horizontal }; + Button button = new Button(); + button.Name = "button1"; + Binding bindingButton = new Binding("alarmTime"); + button.SetBinding(Button.ContentProperty, bindingButton); + stackPanel.Children.Add(button); + + TextBlock textBlock = new TextBlock(); + textBlock.Name = "text1"; + Binding bindingtextBlock = new Binding("alarmMessage"); + textBlock.SetBinding(TextBlock.TextProperty, bindingtextBlock); + stackPanel.Children.Add(textBlock); + FrameworkElementToXml(stackPanel); + + + + var template = (DataTemplate)XamlReader.Parse(@" + + " + ChildTemplateXml + @" +"); + SetValue(ItemTemplateProperty, template); + Binding binding = new Binding(); binding.RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.Self }; binding.Path = new PropertyPath("ItemsString"); + SetBinding(ItemsSourceProperty, binding); + } - SetBinding(ItemsSourceProperty,binding); + public void Dispose() + { + timer.Stop(); + FDataSouce = ""; + //if (IsRun) IsRun = false; + GenerateDataRefresh(); } - private void MyButton_Click(object sender,RoutedEventArgs e) + 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]; + } + } + 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_DeviceData.ContainsKey(str[0])) + { + Dictionary b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]]; + if (b != null && b.ContainsKey(str[1])) + { + FDataSouce = b[str[1]].VarVaule; + } + } + } + } + } + else if (DataSouceType == DataTypeEnum.静态数据) + { + FDataSouce = ""; + } + } + } + + } + + internal class BindingConvertor : ExpressionConverter + { + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + if (destinationType == typeof(MarkupExtension)) + return true; + else return false; + } + public override object ConvertTo(ITypeDescriptorContext context, + System.Globalization.CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(MarkupExtension)) + { + BindingExpression bindingExpression = value as BindingExpression; + if (bindingExpression == null) + throw new Exception(); + return bindingExpression.ParentBinding; + } + return base.ConvertTo(context, culture, value, destinationType); + } + } + + internal static class EditorHelper + { + public static void Register() + { + Attribute[] attr = new Attribute[1]; + TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); + attr[0] = vConv; + TypeDescriptor.AddAttributes(typeof(T), attr); } } } diff --git a/BeDesignerSCADA/Controls/MainCanvasPanel.xaml b/BeDesignerSCADA/Controls/MainCanvasPanel.xaml index b4d9d45b..a39704a5 100644 --- a/BeDesignerSCADA/Controls/MainCanvasPanel.xaml +++ b/BeDesignerSCADA/Controls/MainCanvasPanel.xaml @@ -269,6 +269,9 @@ + + + @@ -316,6 +319,19 @@ + + + + + + + + + + + + + @@ -383,8 +399,14 @@ - - + + + + + + + + diff --git a/BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs b/BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs index b9a03a4e..e896cdbd 100644 --- a/BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs +++ b/BeDesignerSCADA/Controls/MainCanvasPanel.xaml.cs @@ -6,6 +6,7 @@ using BPASmartClient.DATABUS; using BPASmartClient.MessageName; using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; @@ -18,6 +19,7 @@ using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; +using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; @@ -40,7 +42,8 @@ namespace BeDesignerSCADA.Controls public MainCanvasPanel(string _Path) { InitializeComponent(); - Path= _Path; + EditorHelper.Register(); + Path = _Path; this.DataContext = viewModel; viewModel.Loaded(cav, runCanvas); //控件加载 @@ -200,6 +203,44 @@ namespace BeDesignerSCADA.Controls } } + /// + /// 根据流加载数据 + /// + public void LoadFrameworkElement(UIElementCollection frameworks) + { + try + { + cav.Children.Clear(); + foreach (FrameworkElement element in frameworks) + { + string xamlText = XamlWriter.Save(element); + FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; + cav.Children.Add(item); + } + cav.SelectedItems?.Clear(); + } + catch (Exception ex) + { + + throw; + } + } + /// + /// 获取所有控件 + /// + /// + public List GetChildren() + { + List frameworks = new List(); + foreach (FrameworkElement child in cav.Children) + { + string xamlText = XamlWriter.Save(child); + FrameworkElement item = XamlReader.Parse(xamlText) as FrameworkElement; + frameworks.Add(item); + } + return frameworks; + } + /// /// 保存文件 /// @@ -359,6 +400,55 @@ namespace BeDesignerSCADA.Controls } } /// + /// 设置子控件模板 + /// + /// + /// + private void ChildToggleButton_Click(object sender, RoutedEventArgs e) + { + try + { + if (sender is ToggleButton) + { + ToggleButton toggle = (ToggleButton)sender; + Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem; + if (propertyGridCommand != null) + { + viewModel.ChildEdit(propertyGridCommand); + } + } + } + catch (Exception ex) + { + + } + } + /// + /// 数据绑定 + /// + /// + /// + private void BingToggleButton_Click(object sender, RoutedEventArgs e) + { + try + { + if (sender is ToggleButton) + { + + ToggleButton toggle = (ToggleButton)sender; + Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem; + if (propertyGridCommand != null) + { + viewModel.BingEdit(propertyGridCommand); + } + } + } + catch (Exception ex) + { + + } + } + /// /// 变量选择 /// /// @@ -453,6 +543,43 @@ namespace BeDesignerSCADA.Controls } communication?.CommunicationDevices?.ToList().ForEach(x => { viewModel.DevNameList.Add(x.DeviceName); }); } + #endregion + + + } + + internal class BindingConvertor : ExpressionConverter + { + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + if (destinationType == typeof(MarkupExtension)) + return true; + else return false; + } + public override object ConvertTo(ITypeDescriptorContext context, + System.Globalization.CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(MarkupExtension)) + { + BindingExpression bindingExpression = value as BindingExpression; + if (bindingExpression == null) + throw new Exception(); + return bindingExpression.ParentBinding; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + internal static class EditorHelper + { + public static void Register() + { + Attribute[] attr = new Attribute[1]; + TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); + attr[0] = vConv; + TypeDescriptor.AddAttributes(typeof(T), attr); + } } } diff --git a/BeDesignerSCADA/View/ChildEditWindow.xaml b/BeDesignerSCADA/View/ChildEditWindow.xaml new file mode 100644 index 00000000..5f50bd14 --- /dev/null +++ b/BeDesignerSCADA/View/ChildEditWindow.xaml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + +