MfcControl.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Common.DeviceData;
  15. using Aitex.Core.UI.ControlDataContext;
  16. using MECF.Framework.Common.OperationCenter;
  17. namespace Aitex.Core.UI.Control
  18. {
  19. /// <summary>
  20. /// MfcControl.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MfcControl : UserControl
  23. {
  24. public MfcControl()
  25. {
  26. InitializeComponent();
  27. }
  28. static MfcControl()
  29. {
  30. IsEnabledProperty.OverrideMetadata(typeof(MfcControl),
  31. new FrameworkPropertyMetadata(true, new PropertyChangedCallback(IsEnabledPropertyChanged),
  32. new CoerceValueCallback(CoerceIsEnabled)));
  33. }
  34. // define dependency properties
  35. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  36. "Command", typeof(ICommand), typeof(MfcControl),
  37. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  38. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  39. "DeviceData", typeof(AITMfcData), typeof(MfcControl),
  40. new FrameworkPropertyMetadata(new AITMfcData(), FrameworkPropertyMetadataOptions.AffectsRender));
  41. public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
  42. "BackColor", typeof(Brush), typeof(MfcControl),
  43. new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender));
  44. /// <summary>
  45. /// 输入值是否百分比,默认否
  46. /// </summary>
  47. public bool IsPercent { get; set; }
  48. public ICommand Command
  49. {
  50. get
  51. {
  52. return (ICommand)this.GetValue(CommandProperty);
  53. }
  54. set
  55. {
  56. this.SetValue(CommandProperty, value);
  57. }
  58. }
  59. /// <summary>
  60. /// set, get current progress value AnalogDeviceData
  61. /// </summary>
  62. public AITMfcData DeviceData
  63. {
  64. get
  65. {
  66. return (AITMfcData)this.GetValue(DeviceDataProperty);
  67. }
  68. set
  69. {
  70. this.SetValue(DeviceDataProperty, value);
  71. }
  72. }
  73. public Brush BackColor
  74. {
  75. get
  76. {
  77. return (Brush)this.GetValue(BackColorProperty);
  78. }
  79. set
  80. {
  81. this.SetValue(BackColorProperty, value);
  82. }
  83. }
  84. protected override void OnRender(DrawingContext drawingContext)
  85. {
  86. base.OnRender(drawingContext);
  87. //draw background color
  88. rectBkground.Fill = BackColor;
  89. if (DeviceData != null)
  90. {
  91. //draw red board if mfc meets a warning
  92. rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  93. labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  94. rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  95. if (dialogBox != null)
  96. {
  97. if (IsPercent)
  98. dialogBox.RealValue = (DeviceData.FeedBack * 100).ToString("F1") + "%";
  99. else
  100. dialogBox.RealValue = DeviceData.FeedBack.ToString("F1");
  101. }
  102. }
  103. }
  104. private InputDialogBox dialogBox;
  105. public Window AnalogOwner { get; set; }
  106. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  107. {
  108. if (DeviceData == null)
  109. return;
  110. dialogBox = new InputDialogBox
  111. {
  112. CommandDelegate = Execute,
  113. DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
  114. DeviceId = DeviceData.DeviceSchematicId,
  115. DefaultValue = DeviceData.DefaultValue,
  116. RealValue = DeviceData.FeedBack.ToString("F1"),
  117. SetPoint = Math.Round(DeviceData.SetPoint, 1),
  118. MaxValue = DeviceData.Scale,
  119. Unit = DeviceData.Unit,
  120. };
  121. dialogBox.IsPercent = IsPercent;
  122. if (IsPercent)
  123. dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
  124. if (AnalogOwner != null)
  125. dialogBox.Owner = AnalogOwner;
  126. dialogBox.Topmost = true;
  127. dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  128. dialogBox.FocasAll();
  129. dialogBox.ShowDialog();
  130. dialogBox = null;
  131. }
  132. private void Execute(double value)
  133. {
  134. if (Command == null)
  135. {
  136. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITMfcOperation.Ramp}", value, 0);
  137. return;
  138. }
  139. Command.Execute(new object[] { DeviceData.DeviceName, AITMfcOperation.Ramp, value });
  140. }
  141. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  142. {
  143. if (DeviceData != null)
  144. {
  145. string tooltipValue =
  146. 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}",
  147. DeviceData.Type,
  148. DeviceData.DisplayName,
  149. DeviceData.DeviceSchematicId,
  150. DeviceData.Scale,
  151. DeviceData.Unit,
  152. IsPercent ? (DeviceData.SetPoint * 100).ToString("F1") + "%" : DeviceData.SetPoint.ToString("F1"),
  153. IsPercent ? (DeviceData.FeedBack * 100).ToString("F1") + "%" : DeviceData.FeedBack.ToString("F1"),
  154. DeviceData.Scale > 0 ? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString("F1") : "0",
  155. DeviceData.IsWarning ? "Tolerance Warning" : "Normal");
  156. ToolTip = tooltipValue;
  157. }
  158. }
  159. private static void IsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
  160. {
  161. }
  162. private static object CoerceIsEnabled(DependencyObject source, object value)
  163. {
  164. return value;
  165. }
  166. }
  167. }