2025-07-02 18:33:08 +08:00
|
|
|
|
using System.Windows;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
using System.Windows.Controls;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
using System.Windows.Media;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Controls;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
using NLog;
|
|
|
|
|
|
using PMSWPF.Enums;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
using PMSWPF.Helper;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
using PMSWPF.Models;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
using PMSWPF.ViewModels;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Views;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class VariableTableView : UserControl
|
|
|
|
|
|
{
|
2025-07-03 20:12:07 +08:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2025-07-02 18:33:08 +08:00
|
|
|
|
public bool IsLoadCompletion;
|
2025-07-03 20:12:07 +08:00
|
|
|
|
private VariableTableViewModel _viewModel;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
2025-06-30 14:19:22 +08:00
|
|
|
|
public VariableTableView()
|
2025-06-23 17:01:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
2025-07-02 18:33:08 +08:00
|
|
|
|
IsLoadCompletion = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnIsActiveChanged(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-03 20:12:07 +08:00
|
|
|
|
_viewModel = (VariableTableViewModel)this.DataContext;
|
2025-07-02 18:33:08 +08:00
|
|
|
|
// 判断如果没有加载完成就跳过,防止ToggleSwtich加载的时候触发
|
|
|
|
|
|
if (!_viewModel.IsLoadCompletion || !IsLoadCompletion)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
ToggleSwitch toggleSwitch = (ToggleSwitch)sender;
|
|
|
|
|
|
await _viewModel.OnIsActiveChanged(toggleSwitch.IsOn);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError($"修改变量表启用,停用时发生了错误:{exception.Message}", exception);
|
2025-07-02 18:33:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void VariableTableView_OnLoaded(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
IsLoadCompletion = true;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|
2025-07-03 20:12:07 +08:00
|
|
|
|
|
|
|
|
|
|
private void DataGrid_OnCellEditEnding(object? sender, DataGridCellEditEndingEventArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.EditAction != DataGridEditAction.Commit)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取到改变后的值和绑定的属性名
|
|
|
|
|
|
VariableData varData = (VariableData)args.Row.Item;
|
|
|
|
|
|
var element = args.EditingElement;
|
|
|
|
|
|
object newValue = null;
|
|
|
|
|
|
string bindingPath = "";
|
|
|
|
|
|
|
|
|
|
|
|
if (element is TextBox textBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
newValue = textBox.Text;
|
|
|
|
|
|
DataGridTextColumn textColumn = (DataGridTextColumn)args.Column;
|
|
|
|
|
|
bindingPath = (textColumn.Binding as Binding)?.Path.Path;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (element is CheckBox checkBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
newValue = checkBox.IsChecked;
|
|
|
|
|
|
DataGridCheckBoxColumn checkBoxColumn = (DataGridCheckBoxColumn)args.Column;
|
|
|
|
|
|
bindingPath = (checkBoxColumn.Binding as Binding)?.Path.Path;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (args.Column.Header.ToString() == "信号类型")
|
|
|
|
|
|
{
|
|
|
|
|
|
var comboBox = VisualTreeHelper.GetChild(element, 0) as ComboBox;
|
|
|
|
|
|
if (comboBox != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
newValue = comboBox.SelectedItem;
|
|
|
|
|
|
bindingPath = "SignalType";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (newValue == null || string.IsNullOrEmpty(bindingPath))
|
|
|
|
|
|
return;
|
|
|
|
|
|
// 通过反射拿到值
|
|
|
|
|
|
var pathPropertyInfo = varData.GetType()
|
|
|
|
|
|
.GetProperty(bindingPath);
|
|
|
|
|
|
var oldValue = pathPropertyInfo.GetValue(varData);
|
|
|
|
|
|
// 判断值是否相等
|
|
|
|
|
|
if (newValue.ToString() != oldValue?.ToString())
|
|
|
|
|
|
{
|
|
|
|
|
|
varData.IsModified = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("变量表编辑的过过程中发生了错误:" + e.Message, e);
|
2025-07-03 20:12:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 00:18:59 +08:00
|
|
|
|
|
|
|
|
|
|
private async void DeleteVarData_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_viewModel = (VariableTableViewModel)this.DataContext;
|
|
|
|
|
|
var selectedVariables = BasicGridView.SelectedItems.Cast<VariableData>().ToList();
|
|
|
|
|
|
if (selectedVariables.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
await _viewModel.DeleteVarData(selectedVariables);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要删除的变量");
|
2025-07-05 00:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 16:27:21 +08:00
|
|
|
|
|
|
|
|
|
|
private async void ChangePollLevel_Click(object sender, RoutedEventArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_viewModel = (VariableTableViewModel)this.DataContext;
|
|
|
|
|
|
var selectedVariables = BasicGridView.SelectedItems.Cast<VariableData>().ToList();
|
|
|
|
|
|
if (selectedVariables.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
await _viewModel.ChangePollLevel(selectedVariables);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
|
2025-07-05 16:27:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("修改轮询频率的过程中发生了错误:" + e.Message, e);
|
2025-07-05 16:27:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 18:15:21 +08:00
|
|
|
|
|
|
|
|
|
|
private async void AddMqttServerToVariables_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_viewModel = (VariableTableViewModel)this.DataContext;
|
|
|
|
|
|
var selectedVariables = BasicGridView.SelectedItems.Cast<VariableData>().ToList();
|
|
|
|
|
|
if (selectedVariables.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
await _viewModel.AddMqttServerToVariables(selectedVariables);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowInfo("请选择要添加MQTT服务器的变量");
|
2025-07-05 18:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("给变量添加MQTT服务器的过程中发生了错误:" + ex.Message, ex);
|
2025-07-05 18:15:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|