终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

146 行
5.3 KiB

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