diff --git a/src/view/ConfigPage.xaml b/src/view/ConfigPage.xaml index 84109cc..6cb6800 100644 --- a/src/view/ConfigPage.xaml +++ b/src/view/ConfigPage.xaml @@ -215,11 +215,11 @@ Background="#333333" Foreground="White" HorizontalAlignment="Center" Margin="0,30,0,0"/> - + - + - + diff --git a/src/view/ConfigPage.xaml.cs b/src/view/ConfigPage.xaml.cs index b6d99d2..68e1619 100644 --- a/src/view/ConfigPage.xaml.cs +++ b/src/view/ConfigPage.xaml.cs @@ -20,7 +20,6 @@ public partial class ConfigPage : Page public ConfigPage(LogService logService, DatabaseService databaseService, EventService eventService) { InitializeComponent(); - InitializeLevelData(); log = logService; db = databaseService; even = eventService; @@ -37,6 +36,9 @@ public partial class ConfigPage : Page InitializeSensorData(); // 主传感器选择 LoadMainSensorSelectionFromDb(); + // 等级 + InitializeLevelData(); + // 注册等级标签按钮点击事件 LevelTabUnder.Click += LevelTab_Click; @@ -50,7 +52,7 @@ public partial class ConfigPage : Page LevelTabOver.Click += LevelTab_Click; // 默认选中 Under - LevelTabUnder.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); + //LevelTabUnder.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); } @@ -304,36 +306,85 @@ public partial class ConfigPage : Page #region 第五部分 等级 + // 初始化等级数据 private void InitializeLevelData() { levelData = new Dictionary(); + + SqlSugarScope scope = db.GetScope("D1"); + if (scope == null) + { + MessageBox.Show("SqlSugarScope 获取失败!"); + return; + } + + string productName = "DFPV C15TE"; // TODO: 根据实际选择的产品替换 string[] tabs = { "Under", "A", "B", "C", "D", "E", "F", "G", "Over" }; + // 从数据库查询该产品的等级数据 + var sensorsRank = scope.Queryable() + .Where(s => s.ProductName == productName) + .ToList(); + + // 初始化每个等级 foreach (var tab in tabs) { - // 默认数据和 Under 一样 - levelData[tab] = new LevelInfo + var sensorRank = sensorsRank.FirstOrDefault(s => s.Name == tab); + if (sensorRank != null) { - Low = "-0.0090", - High = "0.0090", - Mark = "标记", - Status = "NG" - }; + levelData[tab] = sensorRank; + } + else + { + // 没有数据库记录则初始化默认值 + levelData[tab] = new LevelInfo + { + Name = tab, + Low = "0.0", + High = "0.0", + Mark = "NG", + Status = "NG", + ProductName = productName + }; + } } + + // 默认加载第一个等级 + LoadLevelData(tabs[0]); } + + // 加载数据到界面 + private void LoadLevelData(string sensorIndex) + { + if (!levelData.ContainsKey(sensorIndex)) + return; + + var data = levelData[sensorIndex]; + + // 加强防护,防止 NullReferenceException + if (LowerLimitTextBlock != null) + LowerLimitTextBlock.Text = data.Low ?? "下限"; + + if (HighLimitTextBox != null) + HighLimitTextBox.Text = data.High ?? "0.0"; + + if (UnitMarkTextBlock != null) + UnitMarkTextBlock.Text = data.Mark ?? "NG"; + } + // 等级标签按钮点击事件 private void LevelTab_Click(object sender, RoutedEventArgs e) { if (sender is Button btn && btn.Tag is string tabName) { - // 保存当前显示的数据到上一个等级 + // 保存当前等级修改 SaveCurrentLevelData(); - // 更新标签按钮样式 + // 更新标签样式 UpdateTabStyles(btn); - // 加载选中等级的数据 + // 加载新等级 LoadLevelData(tabName); } } @@ -342,38 +393,35 @@ public partial class ConfigPage : Page private void SaveCurrentLevelData() { string currentTab = GetCurrentSelectedTab(); - if (currentTab == null) return; + if (currentTab == null || !levelData.ContainsKey(currentTab)) + return; - foreach (var child in LevelInfoPanel.Children) - { - if (child is Grid g) - { - foreach (var element in g.Children) - { - if (element is TextBox tb && Grid.GetColumn(tb) == 2 && Grid.GetRow(tb) == 1) - { - levelData[currentTab].Low = tb.Text; - } - 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,可以在这里添加 - } - } - } + var data = levelData[currentTab]; + + // 直接取命名控件的值最稳妥 + if (LowerLimitTextBlock != null) + data.Low = LowerLimitTextBlock.Text ?? "0.0"; + + if (HighLimitTextBox != null) + data.High = HighLimitTextBox.Text ?? "0.0"; + + if (UnitMarkTextBlock != null) + data.Mark = UnitMarkTextBlock.Text ?? "NG"; } // 获取当前选中的标签名 private string GetCurrentSelectedTab() { - foreach (var child in ((Grid)LevelTabUnder.Parent).Children) + if (LevelTabUnder?.Parent is Grid grid) { - if (child is Button b && b.Background is SolidColorBrush brush) + foreach (var child in grid.Children) { - if (brush.Color == ((SolidColorBrush)new BrushConverter().ConvertFrom("#E6F3FF")).Color) - return b.Tag?.ToString(); + if (child is Button b && b.Background is SolidColorBrush brush) + { + var activeColor = (Color)ColorConverter.ConvertFromString("#E6F3FF"); + if (brush.Color == activeColor) + return b.Tag?.ToString(); + } } } return null; @@ -382,47 +430,24 @@ public partial class ConfigPage : Page // 更新等级标签按钮样式 private void UpdateTabStyles(Button selectedBtn) { - foreach (var child in ((Grid)LevelTabUnder.Parent).Children) + if (selectedBtn?.Parent is Grid grid) { - if (child is Button b) + foreach (var child in grid.Children) { - b.Background = (b == selectedBtn) - ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF")) - : new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0")); - } - } - } - - // 加载等级数据到界面 - private void LoadLevelData(string tabName) - { - if (!levelData.ContainsKey(tabName)) return; - - var data = levelData[tabName]; - - foreach (var child in LevelInfoPanel.Children) - { - if (child is Grid g) - { - foreach (var element in g.Children) + if (child is Button b) { - 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) - { - if (br.Child is TextBlock tblock) - tblock.Text = data.Status; - } - // High 或 Mark 可在此扩展 + b.Background = (b == selectedBtn) + ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6F3FF")) // 选中浅蓝 + : new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F0F0")); // 未选灰 } } } } + #endregion + } ///