AITScDoubleRow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace MECF.Framework.UI.Core.CommonControl
  8. {
  9. /// <summary>
  10. /// AITSCRow.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class AITScDoubleRow : UserControl
  13. {
  14. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  15. "Command", typeof(ICommand), typeof(AITScDoubleRow),
  16. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  17. public ICommand Command
  18. {
  19. get
  20. {
  21. return (ICommand)this.GetValue(CommandProperty);
  22. }
  23. set
  24. {
  25. this.SetValue(CommandProperty, value);
  26. }
  27. }
  28. public static readonly DependencyProperty NameWidthProperty = DependencyProperty.Register(
  29. "NameWidth", typeof(int), typeof(AITScDoubleRow),
  30. new FrameworkPropertyMetadata(180, FrameworkPropertyMetadataOptions.AffectsRender));
  31. public int NameWidth
  32. {
  33. get
  34. {
  35. return (int)this.GetValue(NameWidthProperty);
  36. }
  37. set
  38. {
  39. this.SetValue(NameWidthProperty, value);
  40. }
  41. }
  42. public static readonly DependencyProperty FeedbackWidthProperty = DependencyProperty.Register(
  43. "FeedbackWidth", typeof(int), typeof(AITScDoubleRow),
  44. new FrameworkPropertyMetadata(90, FrameworkPropertyMetadataOptions.AffectsRender));
  45. public int FeedbackWidth
  46. {
  47. get
  48. {
  49. return (int)this.GetValue(FeedbackWidthProperty);
  50. }
  51. set
  52. {
  53. this.SetValue(FeedbackWidthProperty, value);
  54. }
  55. }
  56. public static readonly DependencyProperty SetPointWidthProperty = DependencyProperty.Register(
  57. "SetPointWidth", typeof(int), typeof(AITScDoubleRow),
  58. new FrameworkPropertyMetadata(90, FrameworkPropertyMetadataOptions.AffectsRender));
  59. public int SetPointWidth
  60. {
  61. get
  62. {
  63. return (int)this.GetValue(SetPointWidthProperty);
  64. }
  65. set
  66. {
  67. this.SetValue(SetPointWidthProperty, value);
  68. }
  69. }
  70. public static readonly DependencyProperty CommandWidthProperty = DependencyProperty.Register(
  71. "CommandWidth", typeof(int), typeof(AITScDoubleRow),
  72. new FrameworkPropertyMetadata(90, FrameworkPropertyMetadataOptions.AffectsRender));
  73. public int CommandWidth
  74. {
  75. get
  76. {
  77. return (int)this.GetValue(CommandWidthProperty);
  78. }
  79. set
  80. {
  81. this.SetValue(CommandWidthProperty, value);
  82. }
  83. }
  84. public static readonly DependencyProperty ScIdProperty = DependencyProperty.Register(
  85. "ScId", typeof(string), typeof(AITScDoubleRow),
  86. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  87. public string ScId
  88. {
  89. get
  90. {
  91. return (string)this.GetValue(ScIdProperty);
  92. }
  93. set
  94. {
  95. this.SetValue(ScIdProperty, value);
  96. }
  97. }
  98. public static readonly DependencyProperty ScNameProperty = DependencyProperty.Register(
  99. "ScName", typeof(string), typeof(AITScDoubleRow),
  100. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  101. public string ScName
  102. {
  103. get
  104. {
  105. return (string)this.GetValue(ScNameProperty);
  106. }
  107. set
  108. {
  109. this.SetValue(ScNameProperty, value);
  110. }
  111. }
  112. public static readonly DependencyProperty ScFeedbackProperty = DependencyProperty.Register(
  113. "ScFeedback", typeof(double), typeof(AITScDoubleRow),
  114. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  115. public double ScFeedback
  116. {
  117. get
  118. {
  119. return (double)this.GetValue(ScFeedbackProperty);
  120. }
  121. set
  122. {
  123. this.SetValue(ScFeedbackProperty, value);
  124. }
  125. }
  126. public static readonly DependencyProperty ScSetPointProperty = DependencyProperty.Register(
  127. "ScSetPoint", typeof(double), typeof(AITScDoubleRow),
  128. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  129. public double ScSetPoint
  130. {
  131. get
  132. {
  133. return (double)this.GetValue(ScSetPointProperty);
  134. }
  135. set
  136. {
  137. this.SetValue(ScSetPointProperty, value);
  138. }
  139. }
  140. public AITScDoubleRow()
  141. {
  142. InitializeComponent();
  143. }
  144. private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  145. {
  146. e.Handled = !IsTextAllowed(e.Text);
  147. }
  148. protected override void OnRender(DrawingContext drawingContext)
  149. {
  150. base.OnRender(drawingContext);
  151. RectangleSetPoint.Fill = Math.Abs(ScSetPoint - ScFeedback) < 0.001
  152. ? new SolidColorBrush(Color.FromRgb(0x95, 0xB3, 0xD7))
  153. : new SolidColorBrush(Colors.Yellow);
  154. InputSetPoint.Background = Math.Abs(ScSetPoint - ScFeedback) < 0.001
  155. ? new SolidColorBrush(Color.FromRgb(0xDC, 0xF7, 0xF6))
  156. : new SolidColorBrush(Colors.Yellow);
  157. }
  158. private static bool IsTextAllowed(string text)
  159. {
  160. Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
  161. return !regex.IsMatch(text);
  162. }
  163. private void Button_Click(object sender, RoutedEventArgs e)
  164. {
  165. Command.Execute(new object[] { ScId, ScSetPoint.ToString()});
  166. }
  167. }
  168. }