|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using BPASmartClient.Compiler;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
-
- namespace BPASmartClient.SCADAControl.CustomerControls
- {
- /// <summary>
- /// 开关button
- /// </summary>
- [TemplatePart(Name = ELLIPSE, Type = typeof(FrameworkElement))]
- [TemplatePart(Name = TranslateX, Type = typeof(TranslateTransform))]
- public class SwitchButton : ToggleButton, IExecutable
- {
- public event EventHandler PropertyChange; //声明一个事件
-
- public SwitchButton()
- {
- ResourceDictionary languageResDic = new ResourceDictionary();
- languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
- this.Resources.MergedDictionaries.Add(languageResDic);
- SetCurrentValue(WidthProperty, 120d);
- SetCurrentValue(HeightProperty, 40d);
- }
-
- public const string ELLIPSE = "ELLIPSE";
- public const string TranslateX = "TranslateX";
- static SwitchButton()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
- }
- [Category("事件")]
- public string SendText
- {
- get { return (string)GetValue(SendTextProperty); }
- set { SetValue(SendTextProperty, value); }
- }
- public static readonly DependencyProperty SendTextProperty =
- DependencyProperty.Register("SendText", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
- /// <summary>
- /// 不勾选时执行代码
- /// </summary>
- [Category("事件")]
- public string UnCheckedExec
- {
- get { return (string)GetValue(UnCheckedExecProperty); }
- set { SetValue(UnCheckedExecProperty, value); }
- }
- public static readonly DependencyProperty UnCheckedExecProperty =
- DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
- /// <summary>
- /// 勾选时执行代码
- /// </summary>
- [Category("事件")]
- public string CheckedExec
- {
- get { return (string)GetValue(CheckedExecProperty); }
- set { SetValue(CheckedExecProperty, value); }
- }
- public static readonly DependencyProperty CheckedExecProperty =
- DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
- protected override void OnRender(DrawingContext drawingContext)
- {
- base.OnRender(drawingContext);
- ellipse.Width = Height - 8;
- ellipse.Height = Height - 8;
- Refresh();
- }
- /// <summary>
- /// 数据模板
- /// </summary>
- private Dictionary<string, object> DataModel
- {
- get { return (Dictionary<string, object>)GetValue(DataModelProperty); }
- set { SetValue(DataModelProperty, value); }
- }
- private static readonly DependencyProperty DataModelProperty =
- DependencyProperty.Register("DataModel", typeof(Dictionary<string, object>), typeof(SwitchButton), new PropertyMetadata(new Dictionary<string, object>()));
- public Brush DKColor
- {
- get { return (Brush)GetValue(DKColorProperty); }
- set { SetValue(DKColorProperty, value); }
- }
- public static readonly DependencyProperty DKColorProperty =
- DependencyProperty.Register("DKColor", typeof(Brush), typeof(SwitchButton), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
- TranslateTransform transX;
- public Ellipse ellipse;
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
- transX = GetTemplateChild(TranslateX) as TranslateTransform;
- }
- protected override void OnChecked(RoutedEventArgs e)
- {
- base.OnChecked(e);
- Refresh();
- }
- protected override void OnUnchecked(RoutedEventArgs e)
- {
- base.OnUnchecked(e);
- Refresh();
- }
- void Refresh()
- {
-
- if (ellipse == null)
- {
- return;
- }
- DoubleAnimation da = new DoubleAnimation();
- da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- if (IsChecked == true)
- {
- da.To = ActualWidth - ellipse.ActualWidth - 5;
- ellipse.SetCurrentValue(Ellipse.FillProperty, DKColor);
- }
- else
- {
- da.To = 3;
- ellipse.SetCurrentValue(Ellipse.FillProperty, Foreground);
- }
-
- transX.BeginAnimation(TranslateTransform.XProperty, da);
- }
-
- private bool isExecuteState;
- public bool IsExecuteState
- {
- get { return isExecuteState; }
- set
- {
- isExecuteState = value;
- if (IsExecuteState)
- {
- Register();
- }
- }
- }
- /// <summary>
- /// 运行
- /// </summary>
- public void Register()
- {
- Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
- this.Click += SwitchButton_Click;
- if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
- else Config.GetInstance().RunJsScipt(UnCheckedExec);
- Refresh();
- }
- public void BindingActionHeader(object sender, EventArgs e)
- {
- this.Dispatcher.Invoke((Action)(() =>
- {
- DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
- PropertyChange?.Invoke(this, EventArgs.Empty);
- }));
- }
- private void SwitchButton_Click(object sender, RoutedEventArgs e)
- {
- Binding binding = BindingOperations.GetBinding(this, IsCheckedProperty);
- if (binding != null && !string.IsNullOrEmpty(binding.Path.Path))
- {
- string path = binding.Path.Path; string _Name = string.Empty; string _Value = string.Empty;
- if (!string.IsNullOrEmpty(path) && path.Contains("."))
- {
- try
- {
- _Name = path.Split('.')[0].Replace("DataModel[", "").TrimEnd(']');
- _Value = path.Split('.')[1];
- }
- catch (Exception ex)
- {
-
- }
- }
- Dictionary<string, string> blx = new Dictionary<string, string>();
- if (Class_DataBus.GetInstance().Dic_RedisDataType.ContainsKey(_Name))
- blx = Class_DataBus.GetInstance().Dic_RedisDataType[_Name];
- if (blx != null && blx.ContainsKey(_Value))
- {
- SendText = JsonConvert.SerializeObject(new PublishModel
- {
- DeviceName = _Name,
- VarName = _Value,
- Value = this.IsChecked.ToString(),
- DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value])
- });
- }
- }
- if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
- else Config.GetInstance().RunJsScipt(UnCheckedExec);
- }
-
- public string ControlType => "控件";
-
- }
-
-
- }
|