终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

151 satır
4.6 KiB

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