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

164 lines
6.1 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.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using System.Windows.Threading;
  24. namespace BPASmartClient.SCADAControl.CustomerControls
  25. {
  26. /// <summary>
  27. /// TheToggleButton.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheToggleButton : ToggleButton, IExecutable
  30. {
  31. public event EventHandler PropertyChange; //声明一个事件
  32. /// <summary>
  33. /// 开关按钮
  34. /// </summary>
  35. public TheToggleButton()
  36. {
  37. InitializeComponent();
  38. ResourceDictionary languageResDic = new ResourceDictionary();
  39. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
  40. this.Resources.MergedDictionaries.Add(languageResDic);
  41. this.Loaded += TheToggleButton_Loaded;
  42. }
  43. private void TheToggleButton_Loaded(object sender, RoutedEventArgs e)
  44. {
  45. if (this.ActualWidth <= 20)
  46. {
  47. Content = "按钮";
  48. Width = 80;
  49. Height = 30;
  50. }
  51. }
  52. public string ControlType => "控件";
  53. private bool isExecuteState;
  54. public bool IsExecuteState
  55. {
  56. get { return isExecuteState; }
  57. set
  58. {
  59. isExecuteState = value;
  60. if (IsExecuteState)
  61. {
  62. //Style = Application.Current.Resources["ExecuteToggleButton"] as Style;//FindResource("ExecuteToggleButton") as Style;
  63. Register();
  64. }
  65. }
  66. }
  67. [Category("事件")]
  68. public string SendText
  69. {
  70. get { return (string)GetValue(SendTextProperty); }
  71. set { SetValue(SendTextProperty, value); }
  72. }
  73. public static readonly DependencyProperty SendTextProperty =
  74. DependencyProperty.Register("SendText", typeof(string),typeof(TheToggleButton),new PropertyMetadata(string.Empty));
  75. /// <summary>
  76. /// 不勾选时执行代码
  77. /// </summary>
  78. [Category("事件")]
  79. public string UnCheckedExec
  80. {
  81. get { return (string)GetValue(UnCheckedExecProperty); }
  82. set { SetValue(UnCheckedExecProperty, value); }
  83. }
  84. public static readonly DependencyProperty UnCheckedExecProperty =
  85. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(TheToggleButton), new PropertyMetadata(string.Empty));
  86. /// <summary>
  87. /// 勾选时执行代码
  88. /// </summary>
  89. [Category("事件")]
  90. public string CheckedExec
  91. {
  92. get { return (string)GetValue(CheckedExecProperty); }
  93. set { SetValue(CheckedExecProperty, value); }
  94. }
  95. public static readonly DependencyProperty CheckedExecProperty =
  96. DependencyProperty.Register("CheckedExec", typeof(string), typeof(TheToggleButton), new PropertyMetadata(string.Empty));
  97. /// <summary>
  98. /// 数据模板
  99. /// </summary>
  100. private Dictionary<string, object> DataModel
  101. {
  102. get { return (Dictionary<string, object>)GetValue(DataModelProperty); }
  103. set { SetValue(DataModelProperty, value); }
  104. }
  105. private static readonly DependencyProperty DataModelProperty =
  106. DependencyProperty.Register("DataModel", typeof(Dictionary<string, object>), typeof(TheToggleButton), new PropertyMetadata(new Dictionary<string, object>()));
  107. public void Register()
  108. {
  109. Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
  110. Click += TheToggleButton_Click;
  111. }
  112. public void BindingActionHeader(object sender, EventArgs e)
  113. {
  114. this.Dispatcher.Invoke((Action)(() =>
  115. {
  116. DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
  117. PropertyChange?.Invoke(this, EventArgs.Empty);
  118. }));
  119. }
  120. private void TheToggleButton_Click(object sender,RoutedEventArgs e)
  121. {
  122. Binding binding = BindingOperations.GetBinding(this, IsCheckedProperty);
  123. if (binding != null && !string.IsNullOrEmpty(binding.Path.Path))
  124. {
  125. string path = binding.Path.Path; string _Name = string.Empty; string _Value = string.Empty;
  126. if (!string.IsNullOrEmpty(path) && path.Contains("."))
  127. {
  128. try
  129. {
  130. _Name = path.Split('.')[0].Replace("DataModel[", "").TrimEnd(']');
  131. _Value = path.Split('.')[1];
  132. }
  133. catch (Exception ex)
  134. {
  135. }
  136. }
  137. Dictionary<string, string> blx = new Dictionary<string, string>();
  138. if (Class_DataBus.GetInstance().Dic_RedisDataType.ContainsKey(_Name))
  139. blx = Class_DataBus.GetInstance().Dic_RedisDataType[_Name];
  140. if (blx != null && blx.ContainsKey(_Value))
  141. {
  142. SendText = JsonConvert.SerializeObject(new PublishModel
  143. {
  144. DeviceName = _Name,
  145. VarName = _Value,
  146. Value = this.IsChecked.ToString(),
  147. DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value])
  148. });
  149. }
  150. }
  151. if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
  152. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  153. }
  154. }
  155. }