WpfApp/src/view/ConfigPage.xaml.cs

476 lines
13 KiB
C#
Raw Normal View History

2025-10-14 13:11:49 +08:00
using guoke;
2025-10-10 15:58:01 +08:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
2025-10-14 13:44:17 +08:00
namespace WpfApp.src.view;
/// <summary>
/// ConfigPage.xaml 的交互逻辑
/// </summary>
public partial class ConfigPage : Page
2025-10-10 15:58:01 +08:00
{
2025-10-14 13:44:17 +08:00
// 等级数据字典,每个等级对应 LevelInfo
private Dictionary<string, LevelInfo> levelData;
private Dictionary<int, SensorToleranceData> sensorData;
public ConfigPage()
2025-10-10 15:58:01 +08:00
{
2025-10-14 13:44:17 +08:00
InitializeComponent();
InitializeLevelData();
InitializeSensorData();
// 注册等级标签按钮点击事件
LevelTabUnder.Click += LevelTab_Click;
LevelTabA.Click += LevelTab_Click;
LevelTabB.Click += LevelTab_Click;
LevelTabC.Click += LevelTab_Click;
LevelTabD.Click += LevelTab_Click;
LevelTabE.Click += LevelTab_Click;
LevelTabF.Click += LevelTab_Click;
LevelTabG.Click += LevelTab_Click;
LevelTabOver.Click += LevelTab_Click;
// 默认选中 Under
LevelTabUnder.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
// 初始化等级数据
private void InitializeLevelData()
{
levelData = new Dictionary<string, LevelInfo>();
string[] tabs = { "Under", "A", "B", "C", "D", "E", "F", "G", "Over" };
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
foreach (var tab in tabs)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
// 默认数据和 Under 一样
levelData[tab] = new LevelInfo
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
Low = "-0.0090",
High = "0.0090",
Mark = "标记",
Status = "NG"
};
2025-10-10 15:58:01 +08:00
}
2025-10-14 13:44:17 +08:00
}
2025-10-10 15:58:01 +08:00
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 等级标签按钮点击事件
private void LevelTab_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.Tag is string tabName)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
// 保存当前显示的数据到上一个等级
SaveCurrentLevelData();
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 更新标签按钮样式
UpdateTabStyles(btn);
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 加载选中等级的数据
LoadLevelData(tabName);
2025-10-14 13:11:49 +08:00
}
2025-10-14 13:44:17 +08:00
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 保存当前显示的等级数据
private void SaveCurrentLevelData()
{
string currentTab = GetCurrentSelectedTab();
if (currentTab == null) return;
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
foreach (var child in LevelInfoPanel.Children)
{
if (child is Grid g)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
foreach (var element in g.Children)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (element is TextBox tb && Grid.GetColumn(tb) == 2 && Grid.GetRow(tb) == 1)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
levelData[currentTab].Low = tb.Text;
2025-10-14 13:11:49 +08:00
}
2025-10-14 13:44:17 +08:00
if (element is Border br && Grid.GetColumn(br) == 3 && Grid.GetRow(br) == 1)
{
if (br.Child is TextBlock tblock)
levelData[currentTab].Status = tblock.Text;
}
// 如果你后续要保存 High 或 Mark可以在这里添加
2025-10-14 13:11:49 +08:00
}
}
}
2025-10-14 13:44:17 +08:00
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 获取当前选中的标签名
private string GetCurrentSelectedTab()
{
foreach (var child in ((Grid)LevelTabUnder.Parent).Children)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (child is Button b && b.Background is SolidColorBrush brush)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (brush.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color)
return b.Tag?.ToString();
2025-10-14 13:11:49 +08:00
}
}
2025-10-14 13:44:17 +08:00
return null;
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 更新等级标签按钮样式
private void UpdateTabStyles(Button selectedBtn)
{
foreach (var child in ((Grid)LevelTabUnder.Parent).Children)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (child is Button b)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
b.Background = (b == selectedBtn)
? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF"))
: new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0"));
2025-10-14 13:11:49 +08:00
}
}
2025-10-14 13:44:17 +08:00
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 加载等级数据到界面
private void LoadLevelData(string tabName)
{
if (!levelData.ContainsKey(tabName)) return;
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
var data = levelData[tabName];
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
foreach (var child in LevelInfoPanel.Children)
{
if (child is Grid g)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
foreach (var element in g.Children)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (element is TextBox tb && Grid.GetColumn(tb) == 2 && Grid.GetRow(tb) == 1)
{
tb.Text = data.Low;
}
if (element is Border br && Grid.GetColumn(br) == 3 && Grid.GetRow(br) == 1)
2025-10-14 13:11:49 +08:00
{
2025-10-14 13:44:17 +08:00
if (br.Child is TextBlock tblock)
tblock.Text = data.Status;
2025-10-14 13:11:49 +08:00
}
2025-10-14 13:44:17 +08:00
// High 或 Mark 可在此扩展
2025-10-14 13:11:49 +08:00
}
}
}
2025-10-14 13:44:17 +08:00
}
// 初始化传感器数据
private void InitializeSensorData()
{
sensorData = new Dictionary<int, SensorToleranceData>
{
{ 1, new SensorToleranceData { MaxTolerance = "9.00", BaseTolerance = "72.9410", MinTolerance = "-9.00" } },
{ 2, new SensorToleranceData { MaxTolerance = "8.50", BaseTolerance = "72.8500", MinTolerance = "-8.50" } },
{ 3, new SensorToleranceData { MaxTolerance = "7.80", BaseTolerance = "72.7800", MinTolerance = "-7.80" } },
{ 4, new SensorToleranceData { MaxTolerance = "6.90", BaseTolerance = "72.6900", MinTolerance = "-6.90" } }
};
}
// 传感器标签页点击事件处理
private void SensorTab_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
if (clickedButton == null) return;
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
int sensorIndex = int.Parse(clickedButton.Tag.ToString());
2025-10-10 15:58:01 +08:00
// 保存当前传感器的数据
2025-10-14 13:44:17 +08:00
SaveCurrentSensorData();
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
// 更新标签页样式
UpdateTabStyles(sensorIndex);
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
// 加载选中传感器的数据
LoadSensorData(sensorIndex);
}
// 保存当前传感器的数据
private void SaveCurrentSensorData()
{
2025-10-10 15:58:01 +08:00
// 获取当前选中的传感器索引
2025-10-14 13:44:17 +08:00
int currentSensorIndex = GetCurrentSelectedSensorIndex();
if (sensorData.ContainsKey(currentSensorIndex))
2025-10-10 15:58:01 +08:00
{
2025-10-14 13:44:17 +08:00
sensorData[currentSensorIndex].MaxTolerance = MaxToleranceTextBox.Text;
sensorData[currentSensorIndex].BaseTolerance = BaseToleranceTextBox.Text;
sensorData[currentSensorIndex].MinTolerance = MinToleranceTextBox.Text;
2025-10-10 15:58:01 +08:00
}
2025-10-14 13:44:17 +08:00
}
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
// 获取当前选中的传感器索引
private int GetCurrentSelectedSensorIndex()
{
if (SensorTab1.Background is SolidColorBrush brush1 && brush1.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color)
return 1;
if (SensorTab2.Background is SolidColorBrush brush2 && brush2.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color)
return 2;
if (SensorTab3.Background is SolidColorBrush brush3 && brush3.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color)
return 3;
if (SensorTab4.Background is SolidColorBrush brush4 && brush4.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color)
return 4;
return 1; // 默认返回传感器1
}
// 更新标签页样式
private void UpdateTabStyles(int selectedIndex)
{
// 重置所有标签页样式
SensorTab1.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0"));
SensorTab2.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0"));
SensorTab3.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0"));
SensorTab4.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0"));
// 设置选中标签页样式
switch (selectedIndex)
2025-10-10 15:58:01 +08:00
{
2025-10-14 13:44:17 +08:00
case 1:
SensorTab1.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF"));
break;
case 2:
SensorTab2.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF"));
break;
case 3:
SensorTab3.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF"));
break;
case 4:
SensorTab4.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF"));
break;
2025-10-10 15:58:01 +08:00
}
2025-10-14 13:44:17 +08:00
}
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
// 加载传感器数据
private void LoadSensorData(int sensorIndex)
{
if (sensorData.ContainsKey(sensorIndex))
2025-10-10 15:58:01 +08:00
{
2025-10-14 13:44:17 +08:00
var data = sensorData[sensorIndex];
MaxToleranceTextBox.Text = data.MaxTolerance;
BaseToleranceTextBox.Text = data.BaseTolerance;
MinToleranceTextBox.Text = data.MinTolerance;
2025-10-10 15:58:01 +08:00
}
2025-10-14 13:44:17 +08:00
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
}
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
/// <summary>
/// 产品信息数据类
/// </summary>
public class Product
{
/// <summary>
/// 主键 ID
/// </summary>
public int Id { get; set; }
2025-10-10 15:58:01 +08:00
2025-10-14 13:44:17 +08:00
/// <summary>
/// 产品名称
/// </summary>
public string Name { get; set; }
2025-10-14 13:11:49 +08:00
2025-10-14 13:44:17 +08:00
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
2025-10-10 15:58:01 +08:00
}
2025-10-14 13:44:17 +08:00
/// <summary>
/// 传感器选择数据类
/// </summary>
public class SensorSelection
{
/// <summary>
/// 主键 ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 传感器1 名称或编号
/// </summary>
public bool Sensor1 { get; set; }
/// <summary>
/// 传感器2 名称或编号
/// </summary>
public bool Sensor2 { get; set; }
/// <summary>
/// 传感器3 名称或编号
/// </summary>
public bool Sensor3 { get; set; }
/// <summary>
/// 传感器4 名称或编号
/// </summary>
public bool Sensor4 { get; set; }
/// <summary>
/// 所属产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
}
/// <summary>
/// 传感器公差数据类
/// </summary>
public class SensorToleranceData
{
/// <summary>
/// 主键 ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 传感器名称(如 “传感器1”
/// </summary>
public string SensorName { get; set; }
/// <summary>
/// 最大公差
/// </summary>
public string MaxTolerance { get; set; }
/// <summary>
/// 基准公差
/// </summary>
public string BaseTolerance { get; set; }
/// <summary>
/// 最小公差
/// </summary>
public string MinTolerance { get; set; }
/// <summary>
/// 所属产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
}
/// <summary>
/// 主传感器选择数据类
/// </summary>
public class MainSensorSelection
{
/// <summary>
/// 主键 ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 数字传感器1 是否为主传感器
/// </summary>
public bool DigitalSensor1 { get; set; }
/// <summary>
/// 数字传感器2 是否为主传感器
/// </summary>
public bool DigitalSensor2 { get; set; }
/// <summary>
/// 数字传感器3 是否为主传感器
/// </summary>
public bool DigitalSensor3 { get; set; }
/// <summary>
/// 数字传感器4 是否为主传感器
/// </summary>
public bool DigitalSensor4 { get; set; }
/// <summary>
/// 所属产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
}
/// <summary>
/// 等级信息数据类
/// </summary>
public class LevelInfo
{
/// <summary>
/// 主键 ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 等级名称例如A级、B级、C级
/// </summary>
public string Name { get; set; }
/// <summary>
/// 等级下限值
/// </summary>
public string Low { get; set; }
/// <summary>
/// 等级上限值
/// </summary>
public string High { get; set; }
/// <summary>
/// 等级标记(例如:合格、不合格、警告等)
/// </summary>
public string Mark { get; set; }
/// <summary>
/// 当前状态(例如:启用、停用)
/// </summary>
public string Status { get; set; }
/// <summary>
/// 所属产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
}