完成信号类型的绑定,和选择

This commit is contained in:
2025-07-03 20:12:07 +08:00
parent 33fd86db41
commit 6c2e6bd490
4 changed files with 185 additions and 19 deletions

View File

@@ -1,16 +1,23 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using iNKORE.UI.WPF.Modern.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using PMSWPF.Enums;
using PMSWPF.Helper;
using PMSWPF.Models;
using PMSWPF.ViewModels;
namespace PMSWPF.Views;
public partial class VariableTableView : UserControl
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public bool IsLoadCompletion;
private VariableTableViewModel _viewModel;
public VariableTableView()
{
@@ -23,7 +30,7 @@ public partial class VariableTableView : UserControl
{
try
{
var _viewModel = (VariableTableViewModel)this.DataContext;
_viewModel = (VariableTableViewModel)this.DataContext;
// 判断如果没有加载完成就跳过防止ToggleSwtich加载的时候触发
if (!_viewModel.IsLoadCompletion || !IsLoadCompletion)
return;
@@ -41,4 +48,64 @@ public partial class VariableTableView : UserControl
{
IsLoadCompletion = true;
}
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)
{
string msg = "编辑变量表数据时发生了错误:";
Logger.Error(msg + e);
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
}
}
}