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

148 line
5.3 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using BPASmartClient.SCADAControl;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace BPASmartClient.SCADAControl.CustomerControls
  22. {
  23. /// <summary>
  24. /// TheCheckBox.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class TheCheckBox : CheckBox, IExecutable
  27. {
  28. public event EventHandler PropertyChange; //声明一个事件
  29. public TheCheckBox()
  30. {
  31. InitializeComponent();
  32. Content = "勾选框";
  33. VerticalContentAlignment = VerticalAlignment.Center;
  34. }
  35. public string ControlType => "控件";
  36. private bool isExecuteState;
  37. public bool IsExecuteState
  38. {
  39. get { return isExecuteState; }
  40. set
  41. {
  42. isExecuteState = value;
  43. if (IsExecuteState)
  44. {
  45. Register();
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// 不勾选时执行代码
  51. /// </summary>
  52. [Category("事件")]
  53. public string UnCheckedExec
  54. {
  55. get { return (string)GetValue(UnCheckedExecProperty); }
  56. set { SetValue(UnCheckedExecProperty, value); }
  57. }
  58. public static readonly DependencyProperty UnCheckedExecProperty =
  59. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(TheCheckBox), new PropertyMetadata(string.Empty));
  60. /// <summary>
  61. /// 勾选时执行代码
  62. /// </summary>
  63. [Category("事件")]
  64. public string CheckedExec
  65. {
  66. get { return (string)GetValue(CheckedExecProperty); }
  67. set { SetValue(CheckedExecProperty, value); }
  68. }
  69. public static readonly DependencyProperty CheckedExecProperty =
  70. DependencyProperty.Register("CheckedExec", typeof(string), typeof(TheCheckBox), new PropertyMetadata(string.Empty));
  71. [Category("事件")]
  72. public string SendText
  73. {
  74. get { return (string)GetValue(SendTextProperty); }
  75. set { SetValue(SendTextProperty, value); }
  76. }
  77. public static readonly DependencyProperty SendTextProperty =
  78. DependencyProperty.Register("SendText", typeof(string), typeof(TheCheckBox), new PropertyMetadata(string.Empty));
  79. /// <summary>
  80. /// 数据模板
  81. /// </summary>
  82. private Dictionary<string, object> DataModel
  83. {
  84. get { return (Dictionary<string, object>)GetValue(DataModelProperty); }
  85. set { SetValue(DataModelProperty, value); }
  86. }
  87. private static readonly DependencyProperty DataModelProperty =
  88. DependencyProperty.Register("DataModel", typeof(Dictionary<string, object>), typeof(TheCheckBox), new PropertyMetadata(new Dictionary<string, object>()));
  89. public void Register()
  90. {
  91. Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
  92. this.Click += TheCheckBox_Click; ;
  93. }
  94. private void TheCheckBox_Click(object sender, RoutedEventArgs e)
  95. {
  96. Binding binding = BindingOperations.GetBinding(this, IsCheckedProperty);
  97. if (binding != null && !string.IsNullOrEmpty(binding.Path.Path))
  98. {
  99. string path = binding.Path.Path; string _Name = string.Empty; string _Value = string.Empty;
  100. if (!string.IsNullOrEmpty(path) && path.Contains("."))
  101. {
  102. try
  103. {
  104. _Name = path.Split('.')[0].Replace("DataModel[", "").TrimEnd(']');
  105. _Value = path.Split('.')[1];
  106. }
  107. catch (Exception ex)
  108. {
  109. }
  110. }
  111. Dictionary<string, string> blx = new Dictionary<string, string>();
  112. if (Class_DataBus.GetInstance().Dic_RedisDataType.ContainsKey(_Name))
  113. blx = Class_DataBus.GetInstance().Dic_RedisDataType[_Name];
  114. if (blx != null && blx.ContainsKey(_Value))
  115. {
  116. SendText = JsonConvert.SerializeObject(new PublishModel
  117. {
  118. DeviceName = _Name,
  119. VarName = _Value,
  120. Value = this.IsChecked.ToString(),
  121. DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value])
  122. });
  123. }
  124. }
  125. if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
  126. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  127. }
  128. public void BindingActionHeader(object sender, EventArgs e)
  129. {
  130. this.Dispatcher.Invoke((Action)(() =>
  131. {
  132. DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
  133. PropertyChange?.Invoke(this, EventArgs.Empty);
  134. }));
  135. }
  136. }
  137. }