终端一体化运控平台
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.
 
 
 

152 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 event EventHandler PropertyChange; //声明一个事件
  29. public SwitchButton()
  30. {
  31. SetCurrentValue(WidthProperty, 120d);
  32. SetCurrentValue(HeightProperty, 40d);
  33. }
  34. public const string ELLIPSE = "ELLIPSE";
  35. public const string TranslateX = "TranslateX";
  36. static SwitchButton()
  37. {
  38. DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
  39. }
  40. /// <summary>
  41. /// 不勾选时执行代码
  42. /// </summary>
  43. [Category("事件")]
  44. public string UnCheckedExec
  45. {
  46. get { return (string)GetValue(UnCheckedExecProperty); }
  47. set { SetValue(UnCheckedExecProperty, value); }
  48. }
  49. public static readonly DependencyProperty UnCheckedExecProperty =
  50. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  51. /// <summary>
  52. /// 勾选时执行代码
  53. /// </summary>
  54. [Category("事件")]
  55. public string CheckedExec
  56. {
  57. get { return (string)GetValue(CheckedExecProperty); }
  58. set { SetValue(CheckedExecProperty, value); }
  59. }
  60. public static readonly DependencyProperty CheckedExecProperty =
  61. DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  62. protected override void OnRender(DrawingContext drawingContext)
  63. {
  64. base.OnRender(drawingContext);
  65. ellipse.Width = Height - 8;
  66. ellipse.Height = Height - 8;
  67. Refresh();
  68. }
  69. TranslateTransform transX;
  70. Ellipse ellipse;
  71. public override void OnApplyTemplate()
  72. {
  73. base.OnApplyTemplate();
  74. ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
  75. transX = GetTemplateChild(TranslateX) as TranslateTransform;
  76. }
  77. protected override void OnChecked(RoutedEventArgs e)
  78. {
  79. base.OnChecked(e);
  80. Refresh();
  81. }
  82. protected override void OnUnchecked(RoutedEventArgs e)
  83. {
  84. base.OnUnchecked(e);
  85. Refresh();
  86. }
  87. void Refresh()
  88. {
  89. if (ellipse == null)
  90. {
  91. return;
  92. }
  93. DoubleAnimation da = new DoubleAnimation();
  94. da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
  95. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  96. if (IsChecked == true)
  97. {
  98. da.To = ActualWidth - ellipse.ActualWidth - 5;
  99. ellipse.SetCurrentValue(Ellipse.FillProperty, Background);
  100. }
  101. else
  102. {
  103. da.To = 3;
  104. ellipse.SetCurrentValue(Ellipse.FillProperty, Brushes.Gray);
  105. }
  106. transX.BeginAnimation(TranslateTransform.XProperty, da);
  107. }
  108. private bool isExecuteState;
  109. public bool IsExecuteState
  110. {
  111. get { return isExecuteState; }
  112. set
  113. {
  114. isExecuteState = value;
  115. if (IsExecuteState)
  116. {
  117. Register();
  118. }
  119. }
  120. }
  121. public void Register()
  122. {
  123. Checked += TheCheckBox_Checked;
  124. Unchecked += TheCheckBox_Unchecked;
  125. }
  126. private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e)
  127. {
  128. Config.GetInstance().RunJsScipt(UnCheckedExec);
  129. }
  130. private void TheCheckBox_Checked(object sender, RoutedEventArgs e)
  131. {
  132. Config.GetInstance().RunJsScipt(CheckedExec);
  133. }
  134. public string ControlType => "控件";
  135. }
  136. }