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;
namespace BPASmartClient.SCADAControl.CustomerControls
{
///
/// TheToggleButton.xaml 的交互逻辑
///
public partial class TheToggleButton : ToggleButton, IExecutable
{
public event EventHandler PropertyChange; //声明一个事件
///
/// 开关按钮
///
public TheToggleButton()
{
InitializeComponent();
ResourceDictionary languageResDic = new ResourceDictionary();
languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
this.Resources.MergedDictionaries.Add(languageResDic);
this.Loaded += TheToggleButton_Loaded;
}
private void TheToggleButton_Loaded(object sender, RoutedEventArgs e)
{
if (this.ActualWidth <= 20)
{
Content = "按钮";
Width = 80;
Height = 30;
}
}
public string ControlType => "控件";
private bool isExecuteState;
public bool IsExecuteState
{
get { return isExecuteState; }
set
{
isExecuteState = value;
if (IsExecuteState)
{
//Style = Application.Current.Resources["ExecuteToggleButton"] as Style;//FindResource("ExecuteToggleButton") as Style;
Register();
}
}
}
[Category("事件")]
public string SendText
{
get { return (string)GetValue(SendTextProperty); }
set { SetValue(SendTextProperty, value); }
}
public static readonly DependencyProperty SendTextProperty =
DependencyProperty.Register("SendText", typeof(string),typeof(TheToggleButton),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(TheToggleButton), 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(TheToggleButton), new PropertyMetadata(string.Empty));
///
/// 数据模板
///
private Dictionary DataModel
{
get { return (Dictionary)GetValue(DataModelProperty); }
set { SetValue(DataModelProperty, value); }
}
private static readonly DependencyProperty DataModelProperty =
DependencyProperty.Register("DataModel", typeof(Dictionary), typeof(TheToggleButton), new PropertyMetadata(new Dictionary()));
public void Register()
{
Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
Click += TheToggleButton_Click;
}
public void BindingActionHeader(object sender, EventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
PropertyChange?.Invoke(this, EventArgs.Empty);
}));
}
private void TheToggleButton_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);
}
}
}