AnalogControl.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 AnalogControl : UserControl
  23. {
  24. public AnalogControl()
  25. {
  26. InitializeComponent();
  27. }
  28. // define dependency properties
  29. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  30. "Command", typeof(ICommand), typeof(AnalogControl),
  31. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  32. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  33. "DeviceData", typeof(AnalogDeviceDataItem), typeof(AnalogControl),
  34. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender,
  35. new PropertyChangedCallback(OnDeviceDataChanged)));
  36. public static readonly DependencyProperty OperationNameProperty = DependencyProperty.Register(
  37. "OperationName", typeof(string), typeof(AnalogControl),
  38. new FrameworkPropertyMetadata(AnalogDeviceOperation.Ramp, FrameworkPropertyMetadataOptions.AffectsRender));
  39. public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
  40. "BackColor", typeof(Brush), typeof(AnalogControl),
  41. new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender));
  42. public static readonly DependencyProperty HideDialogProperty = DependencyProperty.Register(
  43. "HideDialog", typeof(bool), typeof(AnalogControl),
  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. //draw red board if mfc meets a warning
  118. rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  119. //draw reading value
  120. if (IsPercent)
  121. labelValue.Content = (DeviceData.FeedBack * 100).ToString(DeviceData.FormatString);
  122. else
  123. labelValue.Content = DeviceData.FeedBack.ToString(DeviceData.FormatString);
  124. labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  125. rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  126. if (dialogBox != null)
  127. {
  128. if (IsPercent)
  129. dialogBox.RealValue = (DeviceData.FeedBack * 100).ToString(DeviceData.FormatString) + "%";
  130. else
  131. dialogBox.RealValue = DeviceData.FeedBack.ToString(DeviceData.FormatString);
  132. }
  133. }
  134. }
  135. private InputDialogBox dialogBox;
  136. public Window AnalogOwner { get; set; }
  137. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  138. {
  139. if (DeviceData == null)
  140. return;
  141. if (HideDialog)
  142. return;
  143. dialogBox = new InputDialogBox
  144. {
  145. CommandDelegate = Execute,
  146. DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
  147. DeviceId = DeviceData.DeviceId,
  148. DefaultValue = DeviceData.DefaultValue,
  149. RealValue = DeviceData.FeedBack.ToString(DeviceData.FormatString),
  150. SetPoint = Math.Round(DeviceData.SetPoint, 1),
  151. MaxValue = DeviceData.Scale,
  152. Unit = DeviceData.Unit,
  153. };
  154. dialogBox.IsPercent = IsPercent;
  155. if (IsPercent)
  156. dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
  157. if (AnalogOwner != null)
  158. dialogBox.Owner = AnalogOwner;
  159. dialogBox.Topmost = true;
  160. dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  161. dialogBox.FocasAll();
  162. dialogBox.ShowDialog();
  163. dialogBox = null;
  164. }
  165. private void Execute(double value)
  166. {
  167. Command.Execute(new object[] { DeviceData.DeviceName, OperationName, value });
  168. }
  169. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  170. {
  171. if (DeviceData != null)
  172. {
  173. string tooltipValue =
  174. 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}",
  175. DeviceData.Type,
  176. DeviceData.DisplayName,
  177. DeviceData.DeviceId,
  178. DeviceData.Scale,
  179. DeviceData.Unit,
  180. IsPercent ? (DeviceData.SetPoint * 100).ToString(DeviceData.FormatString) + "%" : DeviceData.SetPoint.ToString(DeviceData.FormatString),
  181. IsPercent ? (DeviceData.FeedBack * 100).ToString(DeviceData.FormatString) + "%" : DeviceData.FeedBack.ToString(DeviceData.FormatString),
  182. DeviceData.Scale > 0 ? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString(DeviceData.FormatString) : "0",
  183. DeviceData.IsWarning ? "Tolerance Warning" : "Normal");
  184. ToolTip = tooltipValue;
  185. }
  186. }
  187. }
  188. }