VacuumPump.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /// Interaction logic for VacuumPump.xaml
  19. /// </summary>
  20. public partial class VacuumPump : UserControl
  21. {
  22. public VacuumPump()
  23. {
  24. InitializeComponent();
  25. }
  26. public static readonly DependencyProperty IsOpenMenuProperty = DependencyProperty.Register(
  27. "IsOpenMenu", typeof(bool), typeof(VacuumPump), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  28. public static readonly DependencyProperty PumpDataProperty = DependencyProperty.Register(
  29. "PumpData", typeof(PumpDataItem), typeof(VacuumPump), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  30. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  31. "Command", typeof(ICommand), typeof(VacuumPump),
  32. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  33. public PumpDataItem PumpData
  34. {
  35. get
  36. {
  37. return (PumpDataItem)GetValue(PumpDataProperty);
  38. }
  39. set
  40. {
  41. SetValue(PumpDataProperty, value);
  42. }
  43. }
  44. public ICommand Command
  45. {
  46. get
  47. {
  48. return (ICommand)GetValue(CommandProperty);
  49. }
  50. set
  51. {
  52. SetValue(CommandProperty, value);
  53. }
  54. }
  55. public bool IsOpenMenu
  56. {
  57. get
  58. {
  59. return (bool)GetValue(IsOpenMenuProperty);
  60. }
  61. set
  62. {
  63. SetValue(IsOpenMenuProperty, value);
  64. }
  65. }
  66. /// <summary>
  67. /// render override
  68. /// </summary>
  69. /// <param name="drawingContext"></param>
  70. protected override void OnRender(DrawingContext drawingContext)
  71. {
  72. base.OnRender(drawingContext);
  73. if (PumpData != null)
  74. {
  75. if (PumpData.MainPumpEnable)
  76. {
  77. EllipseInCircle.Fill = PumpData.IsWarning ? Brushes.Yellow : Brushes.Green;
  78. }
  79. else if (!PumpData.MainPumpEnable)
  80. EllipseInCircle.Fill = Brushes.Red;
  81. else
  82. EllipseInCircle.Fill = Brushes.Gray;
  83. this.ToolTip = PumpData.DisplayName;
  84. }
  85. else
  86. EllipseInCircle.Fill = Brushes.Gray;
  87. }
  88. private void canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  89. {
  90. if(!IsOpenMenu)
  91. { return; }
  92. ContextMenu mouseClickMenu = new ContextMenu();
  93. MenuItem item = new MenuItem();
  94. item.Header = "_" + (PumpData!=null ? PumpData.DisplayName : "未知主泵");
  95. item.Background = Brushes.Gray;
  96. item.Foreground = Brushes.White;
  97. item.IsEnabled = false;
  98. mouseClickMenu.Items.Add(item);
  99. if (PumpData!=null)
  100. {
  101. if (!PumpData.MainPumpEnable)
  102. { addOpenMenu(mouseClickMenu, item); }
  103. else
  104. { addCloseMenu(mouseClickMenu, item); }
  105. }
  106. mouseClickMenu.IsOpen = true;
  107. }
  108. void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item)
  109. {
  110. item = new MenuItem();
  111. item.Header = "打开 (_O)";
  112. item.Click += TurnOnValve;
  113. item.Tag = this.Tag;
  114. mouseClickMenu.Items.Add(item);
  115. }
  116. void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item)
  117. {
  118. item = new MenuItem();
  119. item.Header = "关闭 (_C)";
  120. item.Tag = this.Tag;
  121. item.Click += TurnOffValve;
  122. mouseClickMenu.Items.Add(item);
  123. }
  124. private void TurnOnValve(object sender, RoutedEventArgs e)
  125. {
  126. if (Command != null)
  127. {
  128. Command.Execute(new object[] { PumpData.DeviceName, PumpOperation.PumpON });
  129. }
  130. }
  131. private void TurnOffValve(object sender, RoutedEventArgs e)
  132. {
  133. if (Command != null)
  134. {
  135. Command.Execute(new object[] { PumpData.DeviceName, PumpOperation.PumpOff });
  136. }
  137. }
  138. }
  139. }