终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

150 lines
4.6 KiB

  1. using BPASmartClient.Compiler;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Animation;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace BPASmartClient.SCADAControl.CustomerControls
  20. {
  21. /// <summary>
  22. /// 开关button
  23. /// </summary>
  24. [TemplatePart(Name = ELLIPSE, Type = typeof(FrameworkElement))]
  25. [TemplatePart(Name = TranslateX, Type = typeof(TranslateTransform))]
  26. public class SwitchButton : ToggleButton, IExecutable
  27. {
  28. public SwitchButton()
  29. {
  30. SetCurrentValue(WidthProperty, 120d);
  31. SetCurrentValue(HeightProperty, 40d);
  32. }
  33. public const string ELLIPSE = "ELLIPSE";
  34. public const string TranslateX = "TranslateX";
  35. static SwitchButton()
  36. {
  37. DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
  38. }
  39. /// <summary>
  40. /// 不勾选时执行代码
  41. /// </summary>
  42. [Category("事件")]
  43. public string UnCheckedExec
  44. {
  45. get { return (string)GetValue(UnCheckedExecProperty); }
  46. set { SetValue(UnCheckedExecProperty, value); }
  47. }
  48. public static readonly DependencyProperty UnCheckedExecProperty =
  49. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  50. /// <summary>
  51. /// 勾选时执行代码
  52. /// </summary>
  53. [Category("事件")]
  54. public string CheckedExec
  55. {
  56. get { return (string)GetValue(CheckedExecProperty); }
  57. set { SetValue(CheckedExecProperty, value); }
  58. }
  59. public static readonly DependencyProperty CheckedExecProperty =
  60. DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  61. protected override void OnRender(DrawingContext drawingContext)
  62. {
  63. base.OnRender(drawingContext);
  64. ellipse.Width = Height - 8;
  65. ellipse.Height = Height - 8;
  66. Refresh();
  67. }
  68. TranslateTransform transX;
  69. Ellipse ellipse;
  70. public override void OnApplyTemplate()
  71. {
  72. base.OnApplyTemplate();
  73. ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
  74. transX = GetTemplateChild(TranslateX) as TranslateTransform;
  75. }
  76. protected override void OnChecked(RoutedEventArgs e)
  77. {
  78. base.OnChecked(e);
  79. Refresh();
  80. }
  81. protected override void OnUnchecked(RoutedEventArgs e)
  82. {
  83. base.OnUnchecked(e);
  84. Refresh();
  85. }
  86. void Refresh()
  87. {
  88. if (ellipse == null)
  89. {
  90. return;
  91. }
  92. DoubleAnimation da = new DoubleAnimation();
  93. da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
  94. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  95. if (IsChecked == true)
  96. {
  97. da.To = ActualWidth - ellipse.ActualWidth - 5;
  98. ellipse.SetCurrentValue(Ellipse.FillProperty, Background);
  99. }
  100. else
  101. {
  102. da.To = 3;
  103. ellipse.SetCurrentValue(Ellipse.FillProperty, Brushes.Gray);
  104. }
  105. transX.BeginAnimation(TranslateTransform.XProperty, da);
  106. }
  107. private bool isExecuteState;
  108. public bool IsExecuteState
  109. {
  110. get { return isExecuteState; }
  111. set
  112. {
  113. isExecuteState = value;
  114. if (IsExecuteState)
  115. {
  116. Register();
  117. }
  118. }
  119. }
  120. public void Register()
  121. {
  122. Checked += TheCheckBox_Checked;
  123. Unchecked += TheCheckBox_Unchecked;
  124. }
  125. private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e)
  126. {
  127. Config.GetInstance().RunJsScipt(UnCheckedExec);
  128. }
  129. private void TheCheckBox_Checked(object sender, RoutedEventArgs e)
  130. {
  131. Config.GetInstance().RunJsScipt(CheckedExec);
  132. }
  133. public string ControlType => "控件";
  134. }
  135. }