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

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