using BPASmartClient.Compiler;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace BPASmartClient.SCADAControl.CustomerControls
{
///
/// TheCheckBox.xaml 的交互逻辑
///
public partial class TheCheckBox : CheckBox, IExecutable
{
public event EventHandler PropertyChange; //声明一个事件
public TheCheckBox()
{
InitializeComponent();
Content = "勾选框";
VerticalContentAlignment = VerticalAlignment.Center;
this.Loaded += TheButton_Loaded;
}
private void TheButton_Loaded(object sender, RoutedEventArgs e)
{
if (this.ActualWidth == 54 && this.Content.ToString()== "勾选框")
{
Content = "勾选框";
Width = 60;
Height = 30;
}
}
public string ControlType => "控件";
private bool isExecuteState;
public bool IsExecuteState
{
get { return isExecuteState; }
set
{
isExecuteState = value;
if (IsExecuteState)
{
Register();
}
}
}
///
/// 不勾选时执行代码
///
[Category("事件")]
public string UnCheckedExec
{
get { return (string)GetValue(UnCheckedExecProperty); }
set { SetValue(UnCheckedExecProperty, value); }
}
public static readonly DependencyProperty UnCheckedExecProperty =
DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(TheCheckBox), 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(TheCheckBox), new PropertyMetadata(string.Empty));
[Category("事件")]
public string SendText
{
get { return (string)GetValue(SendTextProperty); }
set { SetValue(SendTextProperty, value); }
}
public static readonly DependencyProperty SendTextProperty =
DependencyProperty.Register("SendText", typeof(string), typeof(TheCheckBox), 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(TheCheckBox), new PropertyMetadata(new Dictionary()));
public void Register()
{
Class_DataBus.GetInstance().BindingAction += BindingActionHeader;
this.Click += TheCheckBox_Click; ;
}
private void TheCheckBox_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 void BindingActionHeader(object sender, EventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding;
PropertyChange?.Invoke(this, EventArgs.Empty);
}));
}
}
}