MfcControl.xaml.cs 5.9 KB

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