终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SwitchButton.cs 4.6 KiB

2 年前
2 年前
2 年前
2 年前
2 年前
2 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }