Pārlūkot izejas kodu

Combox接收额外值

宝臣 王 3 mēneši atpakaļ
vecāks
revīzija
58937129b2

+ 0 - 2
UniformMaterialManagementSystem/App.xaml.cs

@@ -27,8 +27,6 @@ namespace UniformMaterialManagementSystem
 
             using var dbContext = new SqliteContext();
             dbContext.Database.Migrate();
-            dbContext.Dispose();
-
         }
 
         protected override void OnStartup(StartupEventArgs e)

+ 2 - 0
UniformMaterialManagementSystem/Views/InspectionReportPage.xaml

@@ -887,10 +887,12 @@
                                     <DataGridTemplateColumn.CellEditingTemplate>
                                         <DataTemplate>
                                             <ComboBox
+                                                x:Name="Name"
                                                 Height="25"
                                                 Background="White"
                                                 IsEditable="True"
                                                 ItemsSource="{Binding DataContext.UserNames, RelativeSource={RelativeSource AncestorType=UserControl}}"
+                                                LostFocus="Name_OnLostFocus"
                                                 SelectedItem="{Binding Inspector, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                         </DataTemplate>
                                     </DataGridTemplateColumn.CellEditingTemplate>

+ 12 - 1
UniformMaterialManagementSystem/Views/InspectionReportPage.xaml.cs

@@ -1,4 +1,5 @@
-using Microsoft.Extensions.DependencyInjection;
+using System.Windows;
+using Microsoft.Extensions.DependencyInjection;
 using System.Windows.Controls;
 using UniformMaterialManagementSystem.ViewModels;
 
@@ -79,5 +80,15 @@ namespace UniformMaterialManagementSystem.Views
             }
         }
 
+        private void Name_OnLostFocus(object sender, RoutedEventArgs e)
+        {
+            if (sender is not ComboBox comboBox) return;
+            var text = comboBox.Text;
+
+            if (string.IsNullOrWhiteSpace(text) || comboBox.Items.Contains(text) || comboBox.ItemsSource is not List<string> names) return;
+
+            names.Add(text);
+            comboBox.SelectedItem = text;
+        }
     }
 }