RFGenerator.xaml.cs 6.4 KB

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