using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CapMachine.Shared.Controls { /// /// Meter.xaml 的交互逻辑 /// public partial class Meter : UserControl, INotifyPropertyChanged { public Meter() { InitializeComponent(); //ToggleBtnAutoHand.Click += ToggleBtnAutoHand_Click; } /// /// 未使用 直接界面Command绑定AutoHandCommand,请参考界面 /// /// /// private void ToggleBtnAutoHand_Click(object sender, RoutedEventArgs e) { if (AutoHandCommand != null && AutoHandCommand.CanExecute(AutoHandCommandParameter)) { AutoHandCommand.Execute(AutoHandCommandParameter); } } /// /// 仪表名称 /// public string MeterName { get { return (string)base.GetValue(Meter.MeterNameProperty); } set { base.SetValue(Meter.MeterNameProperty, value); } } public static readonly DependencyProperty MeterNameProperty = DependencyProperty.Register("MeterName", typeof(string), typeof(Meter), new PropertyMetadata("名称")); /// /// SV数据 /// public double SVValue { get { return (double)base.GetValue(Meter.SVValueProperty); } set { base.SetValue(Meter.SVValueProperty, value); } } public static readonly DependencyProperty SVValueProperty = DependencyProperty.Register("SVValue", typeof(double), typeof(Meter), new PropertyMetadata(0.0)); /// /// PV数据 /// public double PVValue { get { return (double)base.GetValue(Meter.PVValueProperty); } set { base.SetValue(Meter.PVValueProperty, value); } } public static readonly DependencyProperty PVValueProperty = DependencyProperty.Register("PVValue", typeof(double), typeof(Meter), new PropertyMetadata(0.0)); /// /// 单位 /// public string Unit { get { return (string)base.GetValue(Meter.UnitProperty); } set { base.SetValue(Meter.UnitProperty, value); } } public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(Meter), new PropertyMetadata("")); /// /// 手自动的值 /// public bool AutoHandState { get { return (bool)base.GetValue(Meter.AutoHandStateProperty); } set { base.SetValue(Meter.AutoHandStateProperty, value); } } public static readonly DependencyProperty AutoHandStateProperty = DependencyProperty.Register("AutoHandState", typeof(bool), typeof(Meter), new PropertyMetadata(false)); private string _AutoStateMsg = "自动"; /// /// 手自动的消息 /// public string AutoStateMsg { get { return _AutoStateMsg; } set { _AutoStateMsg = value; OnRaisePropertyChanged("AutoStateMsg"); } } //public string AutoStateMsg { get; set; } = "自动"; /// /// 手自动切换 命令 /// public ICommand AutoHandCommand { get { return (ICommand)GetValue(AutoHandCommandProperty); } set { SetValue(AutoHandCommandProperty, value); } } public static readonly DependencyProperty AutoHandCommandProperty = DependencyProperty.Register("AutoHandCommand", typeof(ICommand), typeof(Meter), new PropertyMetadata(default(ICommand))); /// /// AutoHandCommand 参数 /// public object AutoHandCommandParameter { get { return (object)base.GetValue(Meter.AutoHandCommandParameterProperty); } set { base.SetValue(Meter.AutoHandCommandParameterProperty, value); } } public static readonly DependencyProperty AutoHandCommandParameterProperty = DependencyProperty.Register("AutoHandCommandParameter", typeof(object), typeof(Meter), new PropertyMetadata(true)); //public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler PropertyChanged; /// /// 直接更新数据 /// /// protected void OnRaisePropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// /// 手自动切换 /// /// /// private void ToggleBtnAutoHand_Click_1(object sender, RoutedEventArgs e) { var control = sender as ToggleButton; if (control != null && control.IsChecked == true) { AutoStateMsg = "手动"; IsHandValueShow = "Visible"; } else { AutoStateMsg = "自动"; IsHandValueShow = "Hidden"; } } private string _IsHandValueShow = "Hidden"; /// /// 手动设置的是值是否显示 /// public string IsHandValueShow { get { return _IsHandValueShow; } set { _IsHandValueShow = value; OnRaisePropertyChanged("IsHandValueShow"); } } /// /// 手自给值 命令 /// public ICommand HandValueCommand { get { return (ICommand)GetValue(HandValueCommandProperty); } set { SetValue(HandValueCommandProperty, value); } } public static readonly DependencyProperty HandValueCommandProperty = DependencyProperty.Register("HandValueCommand", typeof(ICommand), typeof(Meter), new PropertyMetadata(default(ICommand))); /// /// 手自给值 参数 MV /// public object HandValueMVParameter { get { return (object)base.GetValue(Meter.HandValueMVParameterProperty); } set { base.SetValue(Meter.HandValueMVParameterProperty, value); } } public static readonly DependencyProperty HandValueMVParameterProperty = DependencyProperty.Register("HandValueMVParameter", typeof(object), typeof(Meter), new PropertyMetadata(0)); /// /// 手自给值 参数 SV /// public object HandValueSVParameter { get { return (object)base.GetValue(Meter.HandValueSVParameterProperty); } set { base.SetValue(Meter.HandValueSVParameterProperty, value); } } public static readonly DependencyProperty HandValueSVParameterProperty = DependencyProperty.Register("HandValueSVParameter", typeof(object), typeof(Meter), new PropertyMetadata(0)); /// /// 手自给值 临时输入用参数 SV /// public object HandValueTempSVParameter { get { return (object)base.GetValue(Meter.HandValueTempSVParameterProperty); } set { base.SetValue(Meter.HandValueTempSVParameterProperty, value); } } public static readonly DependencyProperty HandValueTempSVParameterProperty = DependencyProperty.Register("HandValueTempSVParameter", typeof(object), typeof(Meter), new PropertyMetadata(0)); /// /// 手自给值 临时输入用参数 MV /// public object HandValueTempMVParameter { get { return (object)base.GetValue(Meter.HandValueTempMVParameterProperty); } set { base.SetValue(Meter.HandValueTempMVParameterProperty, value); } } public static readonly DependencyProperty HandValueTempMVParameterProperty = DependencyProperty.Register("HandValueTempMVParameter", typeof(object), typeof(Meter), new PropertyMetadata(0)); /// /// 手动给值回车检测 /// /// /// private void HandValueMV_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (HandValueCommand != null && HandValueCommand.CanExecute(HandValueMVParameter)) { //HandValueParameter 此时无法更新到最新的值(旧值),可能因为RaisePropertyChanged在ViewModel上,不受这边的控制了,所以直接取控件的数据 //HandValueParameter 没有使用,直接取控件的值,HandValueParameter作为初始值使用 if (double.TryParse(HandValueMV.Text, out double Result)) { //把整个控件打包发送过去,无法访问Textbox的值,那么直接传送模型的数据给ViewModel HandValueCommand.Execute(new MeterChannelValue() { Name = MeterName, Value = Result, Type = "MV" }); } } } } /// /// 手动给值回车检测 /// /// /// private void HandValueSV_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (HandValueCommand != null && HandValueCommand.CanExecute(HandValueSVParameter)) { //HandValueTempSVParameter = HandValueSVParameter; //HandValueParameter 此时无法更新到最新的值(旧值),可能因为RaisePropertyChanged在ViewModel上,不受这边的控制了,所以直接取控件的数据 //HandValueParameter 没有使用,直接取控件的值,HandValueParameter作为初始值使用 if (double.TryParse(HandValueSV.Text, out double Result)) { //把整个控件打包发送过去,无法访问Textbox的值,那么直接传送模型的数据给ViewModel HandValueCommand.Execute(new MeterChannelValue() { Name = MeterName, Value = Result, Type = "SV" }); } } } } } }