终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

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