Files
CapMachine/CapMachine.Shared/Controls/Meter.xaml.cs
2024-12-18 15:50:21 +08:00

325 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
/// <summary>
/// Meter.xaml 的交互逻辑
/// </summary>
public partial class Meter : UserControl, INotifyPropertyChanged
{
public Meter()
{
InitializeComponent();
//ToggleBtnAutoHand.Click += ToggleBtnAutoHand_Click;
}
/// <summary>
/// 未使用 直接界面Command绑定AutoHandCommand请参考界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToggleBtnAutoHand_Click(object sender, RoutedEventArgs e)
{
if (AutoHandCommand != null && AutoHandCommand.CanExecute(AutoHandCommandParameter))
{
AutoHandCommand.Execute(AutoHandCommandParameter);
}
}
/// <summary>
/// 仪表名称
/// </summary>
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("名称"));
/// <summary>
/// SV数据
/// </summary>
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));
/// <summary>
/// PV数据
/// </summary>
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));
/// <summary>
/// 单位
/// </summary>
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(""));
/// <summary>
/// 手自动的值
/// </summary>
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 = "自动";
/// <summary>
/// 手自动的消息
/// </summary>
public string AutoStateMsg
{
get { return _AutoStateMsg; }
set
{
_AutoStateMsg = value;
OnRaisePropertyChanged("AutoStateMsg");
}
}
//public string AutoStateMsg { get; set; } = "自动";
/// <summary>
/// 手自动切换 命令
/// </summary>
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)));
/// <summary>
/// AutoHandCommand 参数
/// </summary>
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;
/// <summary>
/// 直接更新数据
/// </summary>
/// <param name="propertyName"></param>
protected void OnRaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// 手自动切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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";
/// <summary>
/// 手动设置的是值是否显示
/// </summary>
public string IsHandValueShow
{
get { return _IsHandValueShow; }
set
{
_IsHandValueShow = value;
OnRaisePropertyChanged("IsHandValueShow");
}
}
/// <summary>
/// 手自给值 命令
/// </summary>
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)));
/// <summary>
/// 手自给值 参数 MV
/// </summary>
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));
/// <summary>
/// 手自给值 参数 SV
/// </summary>
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));
/// <summary>
/// 手动给值回车检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 (int.TryParse(HandValueMV.Text, out int Result))
{
//把整个控件打包发送过去无法访问Textbox的值那么直接传送模型的数据给ViewModel
HandValueCommand.Execute(new MeterChannelValue() { Name = MeterName, Value = Result,Type="MV" });
}
}
}
}
/// <summary>
/// 手动给值回车检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandValueSV_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (HandValueCommand != null && HandValueCommand.CanExecute(HandValueSVParameter))
{
//HandValueParameter 此时无法更新到最新的值(旧值),可能因为RaisePropertyChanged在ViewModel上不受这边的控制了所以直接取控件的数据
//HandValueParameter 没有使用直接取控件的值HandValueParameter作为初始值使用
if (int.TryParse(HandValueSV.Text, out int Result))
{
//把整个控件打包发送过去无法访问Textbox的值那么直接传送模型的数据给ViewModel
HandValueCommand.Execute(new MeterChannelValue() { Name = MeterName, Value = Result, Type = "SV" });
}
}
}
}
}
}