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
{
///
/// 开关button
///
[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));
///
/// 不勾选时执行代码
///
[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));
///
/// 勾选时执行代码
///
[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();
}
///
/// 数据模板
///
private Dictionary DataModel
{
get { return (Dictionary)GetValue(DataModelProperty); }
set { SetValue(DataModelProperty, value); }
}
private static readonly DependencyProperty DataModelProperty =
DependencyProperty.Register("DataModel", typeof(Dictionary), typeof(SwitchButton), new PropertyMetadata(new Dictionary()));
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();
}
}
}
///
/// 运行
///
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 blx = new Dictionary();
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 => "控件";
}
}