AnalogControl.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. private static void OnDeviceDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  43. {
  44. }
  45. /// <summary>
  46. /// 输入值是否百分比,默认否
  47. /// </summary>
  48. public bool IsPercent { get; set; }
  49. public ICommand Command
  50. {
  51. get
  52. {
  53. return (ICommand)this.GetValue(CommandProperty);
  54. }
  55. set
  56. {
  57. this.SetValue(CommandProperty, value);
  58. }
  59. }
  60. /// <summary>
  61. /// set, get current progress value AnalogDeviceData
  62. /// </summary>
  63. public AnalogDeviceDataItem DeviceData
  64. {
  65. get
  66. {
  67. return (AnalogDeviceDataItem)this.GetValue(DeviceDataProperty);
  68. }
  69. set
  70. {
  71. this.SetValue(DeviceDataProperty, value);
  72. }
  73. }
  74. public Brush BackColor
  75. {
  76. get
  77. {
  78. return (Brush)this.GetValue(BackColorProperty);
  79. }
  80. set
  81. {
  82. this.SetValue(BackColorProperty, value);
  83. }
  84. }
  85. public string OperationName
  86. {
  87. get
  88. {
  89. return (string)this.GetValue(OperationNameProperty);
  90. }
  91. set
  92. {
  93. this.SetValue(OperationNameProperty, value);
  94. }
  95. }
  96. protected override void OnRender(DrawingContext drawingContext)
  97. {
  98. base.OnRender(drawingContext);
  99. //draw background color
  100. rectBkground.Fill = BackColor;
  101. if (DeviceData != null)
  102. {
  103. //draw red board if mfc meets a warning
  104. rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  105. //draw reading value
  106. if (IsPercent)
  107. labelValue.Content = (DeviceData.FeedBack * 100).ToString("F1");
  108. else
  109. labelValue.Content = DeviceData.FeedBack.ToString("F1");
  110. labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  111. rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  112. if (dialogBox != null)
  113. {
  114. if (IsPercent)
  115. dialogBox.RealValue = (DeviceData.FeedBack * 100).ToString("F1") + "%";
  116. else
  117. dialogBox.RealValue = DeviceData.FeedBack.ToString("F1");
  118. }
  119. }
  120. }
  121. private InputDialogBox dialogBox;
  122. public Window AnalogOwner { get; set; }
  123. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  124. {
  125. if (DeviceData == null)
  126. return;
  127. dialogBox = new InputDialogBox
  128. {
  129. CommandDelegate = Execute,
  130. DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
  131. DeviceId = DeviceData.DeviceId,
  132. DefaultValue = DeviceData.DefaultValue,
  133. RealValue = DeviceData.FeedBack.ToString("F1"),
  134. SetPoint = Math.Round(DeviceData.SetPoint, 1),
  135. MaxValue = DeviceData.Scale,
  136. Unit = DeviceData.Unit,
  137. };
  138. dialogBox.IsPercent = IsPercent;
  139. if (IsPercent)
  140. dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
  141. if (AnalogOwner != null)
  142. dialogBox.Owner = AnalogOwner;
  143. dialogBox.Topmost = true;
  144. dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  145. dialogBox.FocasAll();
  146. dialogBox.ShowDialog();
  147. dialogBox = null;
  148. }
  149. private void Execute(double value)
  150. {
  151. Command.Execute(new object[] { DeviceData.DeviceName, OperationName, value });
  152. }
  153. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  154. {
  155. if (DeviceData != null)
  156. {
  157. string tooltipValue =
  158. string.Format("{0}:{1}\r\n\r\n编号:{2}\r\n量程:{3} {4}\r\n设定:{5} {4} \r\n反馈:{6} {4}\r\n偏差:{7}%\r\n状态:{8}",
  159. DeviceData.Type,
  160. DeviceData.DisplayName,
  161. DeviceData.DeviceId,
  162. DeviceData.Scale,
  163. DeviceData.Unit,
  164. IsPercent ? (DeviceData.SetPoint * 100).ToString("F1") + "%" : DeviceData.SetPoint.ToString("F1"),
  165. IsPercent ? (DeviceData.FeedBack * 100).ToString("F1") + "%" : DeviceData.FeedBack.ToString("F1"),
  166. DeviceData.Scale > 0 ? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString("F1") : "0",
  167. DeviceData.IsWarning ? "偏差报警" : "正常");
  168. ToolTip = tooltipValue;
  169. }
  170. }
  171. }
  172. }