终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

169 lines
5.9 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.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using System.Windows.Threading;
  23. namespace BPASmartClient.SCADAControl.CustomerControls
  24. {
  25. public class TheTextBox : TextBox, IExecutable
  26. {
  27. static TheTextBox()
  28. {
  29. DefaultStyleKeyProperty.OverrideMetadata(typeof(TheTextBox), new FrameworkPropertyMetadata(typeof(TheTextBox)));
  30. }
  31. public TheTextBox()
  32. {
  33. ResourceDictionary languageResDic = new ResourceDictionary();
  34. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
  35. this.Resources.MergedDictionaries.Add(languageResDic);
  36. this.KeyDown += TheTextBox_KeyDown;
  37. this.TextChanged += TheTextBox_TextChanged;
  38. this.Loaded += TheTextBox_Loaded;
  39. }
  40. private void TheTextBox_Loaded(object sender, RoutedEventArgs e)
  41. {
  42. if (this.ActualWidth <= 10)
  43. {
  44. Text = "文本框";
  45. Height = 30;
  46. Width = 80;
  47. FontSize = 16;
  48. }
  49. }
  50. bool isRun=false;
  51. private void TheTextBox_KeyDown(object sender,KeyEventArgs e)
  52. {
  53. isRun = true;
  54. }
  55. private void TheTextBox_TextChanged(object sender,TextChangedEventArgs e)
  56. {
  57. if (isRun)
  58. {
  59. Binding binding = BindingOperations.GetBinding(this, TextProperty);
  60. if (binding != null && !string.IsNullOrEmpty(binding.Path.Path))
  61. {
  62. string path = binding.Path.Path; string _Name = string.Empty; string _Value = string.Empty;
  63. if (!string.IsNullOrEmpty(path) && path.Contains("."))
  64. {
  65. try
  66. {
  67. _Name = path.Split('.')[0].Replace("DataModel[", "").TrimEnd(']');
  68. _Value = path.Split('.')[1];
  69. }
  70. catch (Exception ex)
  71. {
  72. }
  73. }
  74. Dictionary<string, string> blx = new Dictionary<string, string>();
  75. if (Class_DataBus.GetInstance().Dic_RedisDataType.ContainsKey(_Name))
  76. blx = Class_DataBus.GetInstance().Dic_RedisDataType[_Name];
  77. if (blx != null && blx.ContainsKey(_Value))
  78. {
  79. SendText = JsonConvert.SerializeObject(new PublishModel
  80. {
  81. DeviceName = _Name,
  82. VarName = _Value,
  83. Value = this.Text.ToString(),
  84. DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value])
  85. });
  86. }
  87. }
  88. Config.GetInstance().RunJsScipt(ValueChangedExecute);
  89. isRun=false;
  90. }
  91. }
  92. public string ControlType => "控件";
  93. private bool isExecuteState;
  94. public bool IsExecuteState
  95. {
  96. get { return isExecuteState; }
  97. set
  98. {
  99. isExecuteState = value;
  100. if (IsExecuteState)
  101. {
  102. IsEnabled = true;
  103. Focusable = true;
  104. Register();
  105. }
  106. }
  107. }
  108. #region 数据绑定模块
  109. public string ValueChangedExecute
  110. {
  111. get { return (string)GetValue(ValueChangedExecuteProperty); }
  112. set { SetValue(ValueChangedExecuteProperty,value); }
  113. }
  114. public static readonly DependencyProperty ValueChangedExecuteProperty =
  115. DependencyProperty.Register("ValueChangedExecute",typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty));
  116. [Category("事件")]
  117. public string SendText
  118. {
  119. get { return (string)GetValue(SendTextProperty); }
  120. set { SetValue(SendTextProperty, value); }
  121. }
  122. public static readonly DependencyProperty SendTextProperty =
  123. DependencyProperty.Register("SendText", typeof(string),typeof(TheTextBox),new PropertyMetadata(string.Empty));
  124. public event EventHandler PropertyChange; //声明一个事件
  125. /// <summary>
  126. /// 数据模板
  127. /// </summary>
  128. private Dictionary<string, object> DataModel
  129. {
  130. get { return (Dictionary<string, object>)GetValue(DataModelProperty); }
  131. set { SetValue(DataModelProperty, value); }
  132. }
  133. private static readonly DependencyProperty DataModelProperty =
  134. DependencyProperty.Register("DataModel", typeof(Dictionary<string, object>), typeof(TheTextBox), new PropertyMetadata(new Dictionary<string, object>()));
  135. /// <summary>
  136. /// 运行事件
  137. /// </summary>
  138. public void Register()
  139. {
  140. Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
  141. }
  142. public void BindingActionHeader(object sender, EventArgs e)
  143. {
  144. this.Dispatcher.Invoke((Action)(() =>
  145. {
  146. DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
  147. PropertyChange?.Invoke(this, EventArgs.Empty);
  148. }));
  149. }
  150. #endregion
  151. }
  152. }