AnalogControl2.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.Core.UI.Control;
  15. using Aitex.Core.Util;
  16. using Aitex.Core.UI.ControlDataContext;
  17. namespace Aitex.Core.UI.Control
  18. {
  19. /// <summary>
  20. /// Interaction logic for AnalogControl.xaml
  21. /// </summary>
  22. public partial class AnalogControl2 : UserControl
  23. {
  24. public AnalogControl2()
  25. {
  26. InitializeComponent();
  27. }
  28. // define dependency properties
  29. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  30. "Command", typeof(ICommand), typeof(AnalogControl2),
  31. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  32. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  33. "DeviceData", typeof(AnalogDeviceDataItem), typeof(AnalogControl2),
  34. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender,
  35. new PropertyChangedCallback(OnDeviceDataChanged)));
  36. public static readonly DependencyProperty OperationNameProperty = DependencyProperty.Register(
  37. "OperationName", typeof(string), typeof(AnalogControl2),
  38. new FrameworkPropertyMetadata(AnalogDeviceOperation.Ramp, FrameworkPropertyMetadataOptions.AffectsRender));
  39. public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
  40. "BackColor", typeof(Brush), typeof(AnalogControl2),
  41. new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender));
  42. public static readonly DependencyProperty HideDialogProperty = DependencyProperty.Register(
  43. "HideDialog", typeof(bool), typeof(AnalogControl2),
  44. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  45. private static void OnDeviceDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  46. {
  47. }
  48. /// <summary>
  49. /// 输入值是否百分比,默认否
  50. /// </summary>
  51. public bool IsPercent { get; set; }
  52. public ICommand Command
  53. {
  54. get
  55. {
  56. return (ICommand)this.GetValue(CommandProperty);
  57. }
  58. set
  59. {
  60. this.SetValue(CommandProperty, value);
  61. }
  62. }
  63. /// <summary>
  64. /// set, get current progress value AnalogDeviceData
  65. /// </summary>
  66. public AnalogDeviceDataItem DeviceData
  67. {
  68. get
  69. {
  70. return (AnalogDeviceDataItem)this.GetValue(DeviceDataProperty);
  71. }
  72. set
  73. {
  74. this.SetValue(DeviceDataProperty, value);
  75. }
  76. }
  77. public Brush BackColor
  78. {
  79. get
  80. {
  81. return (Brush)this.GetValue(BackColorProperty);
  82. }
  83. set
  84. {
  85. this.SetValue(BackColorProperty, value);
  86. }
  87. }
  88. public string OperationName
  89. {
  90. get
  91. {
  92. return (string)this.GetValue(OperationNameProperty);
  93. }
  94. set
  95. {
  96. this.SetValue(OperationNameProperty, value);
  97. }
  98. }
  99. public bool HideDialog
  100. {
  101. get
  102. {
  103. return (bool)this.GetValue(HideDialogProperty);
  104. }
  105. set
  106. {
  107. this.SetValue(HideDialogProperty, value);
  108. }
  109. }
  110. protected override void OnRender(DrawingContext drawingContext)
  111. {
  112. base.OnRender(drawingContext);
  113. //draw background color
  114. rectBkground.Fill = BackColor;
  115. if (DeviceData != null)
  116. {
  117. #region FeedBack
  118. //draw red board if mfc meets a warning
  119. rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  120. //draw reading value
  121. if (IsPercent)
  122. labelValue.Content = (DeviceData.FeedBack * 100).ToString("F1");
  123. else
  124. labelValue.Content = DeviceData.FeedBack.ToString("F1");
  125. labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  126. rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  127. if (dialogBox != null)
  128. {
  129. if (IsPercent)
  130. dialogBox.RealValue = (DeviceData.FeedBack * 100).ToString("F1") + "%";
  131. else
  132. dialogBox.RealValue = DeviceData.FeedBack.ToString("F1");
  133. }
  134. #endregion
  135. #region SetPoint
  136. //draw red board if mfc meets a warning
  137. rectSetPoint.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  138. //draw reading value
  139. if (IsPercent)
  140. labelSetPoint.Content = (DeviceData.SetPoint * 100).ToString("F1");
  141. else
  142. labelSetPoint.Content = DeviceData.SetPoint.ToString("F1");
  143. labelSetPoint.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  144. rectSetPoint.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  145. #endregion
  146. }
  147. }
  148. private InputDialogBox dialogBox;
  149. public Window AnalogOwner { get; set; }
  150. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  151. {
  152. if (DeviceData == null)
  153. return;
  154. if (HideDialog)
  155. return;
  156. dialogBox = new InputDialogBox
  157. {
  158. CommandDelegate = Execute,
  159. DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
  160. DeviceId = DeviceData.DeviceId,
  161. DefaultValue = DeviceData.DefaultValue,
  162. RealValue = DeviceData.FeedBack.ToString("F1"),
  163. SetPoint = Math.Round(DeviceData.SetPoint, 1),
  164. MaxValue = DeviceData.Scale,
  165. Unit = DeviceData.Unit,
  166. };
  167. dialogBox.IsPercent = IsPercent;
  168. if (IsPercent)
  169. dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
  170. if (AnalogOwner != null)
  171. dialogBox.Owner = AnalogOwner;
  172. dialogBox.Topmost = true;
  173. dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  174. dialogBox.FocasAll();
  175. dialogBox.ShowDialog();
  176. dialogBox = null;
  177. }
  178. private void Execute(double value)
  179. {
  180. Command.Execute(new object[] { DeviceData.DeviceName, OperationName, value });
  181. }
  182. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  183. {
  184. if (DeviceData != null)
  185. {
  186. string tooltipValue =
  187. string.Format("{0}:{1}\r\n\r\nID:{2}\r\nScale:{3} {4}\r\nSetPoint:{5} {4} \r\nFeedback:{6} {4}\r\nTolerance:{7}%\r\nStatus:{8}",
  188. DeviceData.Type,
  189. DeviceData.DisplayName,
  190. DeviceData.DeviceId,
  191. DeviceData.Scale,
  192. DeviceData.Unit,
  193. IsPercent ? (DeviceData.SetPoint * 100).ToString("F1") + "%" : DeviceData.SetPoint.ToString("F1"),
  194. IsPercent ? (DeviceData.FeedBack * 100).ToString("F1") + "%" : DeviceData.FeedBack.ToString("F1"),
  195. DeviceData.Scale > 0 ? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString("F1") : "0",
  196. DeviceData.IsWarning ? "Tolerance Warning" : "Normal");
  197. ToolTip = tooltipValue;
  198. }
  199. }
  200. }
  201. }