PMCounterValueTextBox.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using SciChart.Core.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace MECF.Framework.UI.Core.Control
  18. {
  19. /// <summary>
  20. /// PMCounterValueTextBox.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class PMCounterValueTextBox : UserControl
  23. {
  24. public PMCounterValueTextBox()
  25. {
  26. InitializeComponent();
  27. }
  28. public static readonly DependencyProperty KeyOperationProperty = DependencyProperty.Register("KeyOperation", typeof(ICommand), typeof(PMCounterValueTextBox));
  29. public ICommand KeyOperation
  30. {
  31. get
  32. {
  33. return (ICommand)this.GetValue(KeyOperationProperty);
  34. }
  35. set
  36. {
  37. this.SetValue(KeyOperationProperty, value);
  38. }
  39. }
  40. public static readonly DependencyProperty PMCounterNodeNameProperty = DependencyProperty.Register(
  41. "PMCounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  42. /// <summary>
  43. /// PMCounter名称
  44. /// </summary>
  45. public string PMCounterNodeName
  46. {
  47. get
  48. {
  49. return (string)this.GetValue(PMCounterNodeNameProperty);
  50. }
  51. set
  52. {
  53. this.SetValue(PMCounterNodeNameProperty, value);
  54. }
  55. }
  56. public static readonly DependencyProperty CounterNodeNameProperty = DependencyProperty.Register(
  57. "CounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  58. /// <summary>
  59. /// PMcounter项目名称
  60. /// </summary>
  61. public string CounterNodeName
  62. {
  63. get
  64. {
  65. return (string)this.GetValue(CounterNodeNameProperty);
  66. }
  67. set
  68. {
  69. this.SetValue(CounterNodeNameProperty, value);
  70. }
  71. }
  72. public static readonly DependencyProperty CounterNodeItemIndexProperty = DependencyProperty.Register(
  73. "CounterNodeItemIndex", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  74. /// <summary>
  75. /// PMcounterItem Index
  76. /// </summary>
  77. public string CounterNodeItemIndex
  78. {
  79. get
  80. {
  81. return (string)this.GetValue(CounterNodeItemIndexProperty);
  82. }
  83. set
  84. {
  85. this.SetValue(CounterNodeItemIndexProperty, value);
  86. }
  87. }
  88. public static readonly DependencyProperty IsHeaderProperty = DependencyProperty.Register(
  89. "IsHeader", typeof(bool), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  90. /// <summary>
  91. /// IsHeader
  92. /// </summary>
  93. public bool IsHeader
  94. {
  95. get
  96. {
  97. return (bool)this.GetValue(IsHeaderProperty);
  98. }
  99. set
  100. {
  101. this.SetValue(IsHeaderProperty, value);
  102. }
  103. }
  104. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  105. "Value", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  106. /// <summary>
  107. /// 数值
  108. /// </summary>
  109. public string Value
  110. {
  111. get
  112. {
  113. return (string)this.GetValue(ValueProperty);
  114. }
  115. set
  116. {
  117. this.SetValue(ValueProperty, value);
  118. }
  119. }
  120. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  121. {
  122. if (e.Key == Key.Enter)
  123. {
  124. if (KeyOperation != null)
  125. {
  126. if (txtInput.Text.IsNullOrEmpty())
  127. {
  128. txtInput.Text = "0";
  129. }
  130. Value = txtInput.Text;
  131. KeyOperation.Execute(new object[] { PMCounterNodeName, CounterNodeName , txtInput.Text });
  132. }
  133. }
  134. }
  135. private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  136. {
  137. e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
  138. }
  139. private bool IsTextAllowed(string key, string text)
  140. {
  141. Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
  142. return !regex.IsMatch(key);
  143. }
  144. private void TextBox_LostFocus(object sender, RoutedEventArgs e)
  145. {
  146. TextBox textBox = sender as TextBox;
  147. if (textBox != null && string.IsNullOrEmpty(textBox.Text))
  148. {
  149. textBox.Text = Value;
  150. }
  151. if (Value != textBox.Text)
  152. {
  153. textBox.Text = Value;
  154. }
  155. }
  156. }
  157. }