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; using System.Windows.Input; namespace BPASmartClient.SCADAControl.CustomerControls { public class TheTextBox : TextBox, IExecutable { static TheTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TheTextBox), new FrameworkPropertyMetadata(typeof(TheTextBox))); } public TheTextBox() { ResourceDictionary languageResDic = new ResourceDictionary(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute); this.Resources.MergedDictionaries.Add(languageResDic); this.KeyDown += TheTextBox_KeyDown; this.TextChanged += TheTextBox_TextChanged; this.Loaded += TheTextBox_Loaded; } private void TheTextBox_Loaded(object sender, RoutedEventArgs e) { if (this.ActualWidth <= 10) { Text = "文本框"; Height = 30; Width = 80; FontSize = 16; } } bool isRun=false; private void TheTextBox_KeyDown(object sender,KeyEventArgs e) { isRun = true; } private void TheTextBox_TextChanged(object sender,TextChangedEventArgs e) { if (isRun) { Binding binding = BindingOperations.GetBinding(this, TextProperty); 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.Text.ToString(), DataType = (EDataType)Enum.Parse(typeof(EDataType), blx[_Value]) }); } } Config.GetInstance().RunJsScipt(ValueChangedExecute); isRun=false; } } public string ControlType => "控件"; private bool isExecuteState; public bool IsExecuteState { get { return isExecuteState; } set { isExecuteState = value; if (IsExecuteState) { IsEnabled = true; Focusable = true; Register(); } } } #region 数据绑定模块 public string ValueChangedExecute { get { return (string)GetValue(ValueChangedExecuteProperty); } set { SetValue(ValueChangedExecuteProperty,value); } } public static readonly DependencyProperty ValueChangedExecuteProperty = DependencyProperty.Register("ValueChangedExecute",typeof(string),typeof(TheTextBox),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(TheTextBox),new PropertyMetadata(string.Empty)); public event EventHandler PropertyChange; //声明一个事件 /// /// 数据模板 /// private Dictionary DataModel { get { return (Dictionary)GetValue(DataModelProperty); } set { SetValue(DataModelProperty, value); } } private static readonly DependencyProperty DataModelProperty = DependencyProperty.Register("DataModel", typeof(Dictionary), typeof(TheTextBox), new PropertyMetadata(new Dictionary())); /// /// 运行事件 /// public void Register() { Class_DataBus.GetInstance().BindingAction += BindingActionHeader; } public void BindingActionHeader(object sender, EventArgs e) { this.Dispatcher.Invoke((Action)(() => { DataModel = Class_DataBus.GetInstance().Dic_RedisDataBinding; PropertyChange?.Invoke(this, EventArgs.Empty); })); } #endregion } }