Эх сурвалжийг харах

Добавление пользователя

Pavel Nagornev 2 жил өмнө
parent
commit
55602a2cfc

+ 15 - 2
DemoExam/ViewModel/ViewModel.cs

@@ -8,6 +8,7 @@ using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
 using System.Windows.Input;
 
 namespace DemoExam.ViewModel
@@ -17,7 +18,7 @@ namespace DemoExam.ViewModel
 
         public event PropertyChangedEventHandler PropertyChanged;
         public Entities DataBase = new Entities();
-        private ObservableCollection<Agent> outPutList;
+        public ObservableCollection<Agent> outPutList;
 
 
         public ObservableCollection<Agent> GetListAgent
@@ -123,7 +124,15 @@ namespace DemoExam.ViewModel
             }
             else
             {
-                outPutList = new ObservableCollection<Agent>(outPutList.Where(x => x.AgentType.Title == filtString));
+                try
+                {
+                    outPutList = new ObservableCollection<Agent>(outPutList.Where(x => x.AgentType.Title == filtString));
+                }
+                catch
+                {
+                    MessageBox.Show("Произшла ошибка. Перезапустите приложение для корректной работы");
+                }
+                
             }
 
         }
@@ -241,6 +250,10 @@ namespace DemoExam.ViewModel
             {
                 if (selectedAgentType == "")
                 {
+                    if (SelectedAgent.AgentType == null)
+                    {
+                        return "";
+                    }
                     return SelectedAgent.AgentType.Title.ToString();
                 }
                 return selectedAgentType;

+ 2 - 0
DemoExam/pages/agents/PageAgents.xaml

@@ -89,6 +89,8 @@
 
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                 <Button Content="Изменить на..." Style="{StaticResource stlButtonChange}" Width="250" Height="30" Click="Button_ChangePriority_Click"></Button>
+
+                <Button Width="250" Height="30" Click="Button_AddAgent_Click">Добавить пользователя</Button>
             </StackPanel>
         </StackPanel>
         <ListBox x:Name="lstAgents" Grid.Row="1" ItemTemplate="{StaticResource listAgentTmpl}" ItemsSource="{Binding GetListAgent}" HorizontalContentAlignment="Center" SelectionMode="Extended" SelectionChanged="lstAgents_SelectionChanged" SelectedItem="{Binding SelectedAgent}" MouseDoubleClick="lstAgents_MouseDoubleClick"></ListBox>

+ 9 - 0
DemoExam/pages/agents/PageAgents.xaml.cs

@@ -52,5 +52,14 @@ namespace DemoExam.pages.agents
             var windowEditAgent = new WindowEditAgent(agentsViewModel);
             windowEditAgent.ShowDialog();
         }
+
+        private void Button_AddAgent_Click(object sender, RoutedEventArgs e)
+        {
+            agentsViewModel.SelectedAgent = new Model.Agent();
+            //agentsViewModel.DataBase.Agent.Add(agentsViewModel.SelectedAgent);
+            var windowEditAgent = new WindowEditAgent(agentsViewModel);
+            windowEditAgent.Title = "Добавление пользователя";
+            windowEditAgent.ShowDialog();
+        }
     }
 }

+ 1 - 1
DemoExam/windows/WindowEditAgent.xaml

@@ -7,7 +7,7 @@
         mc:Ignorable="d"
         Title="Настройки агента" Height="650" Width="400"
         Icon="..\images\logo.ico"
-        
+        Loaded="Window_Loaded"
         MinHeight="650"
         MaxHeight="650"
         MinWidth="400"

+ 43 - 3
DemoExam/windows/WindowEditAgent.xaml.cs

@@ -20,6 +20,7 @@ namespace DemoExam.windows
     public partial class WindowEditAgent : Window
     {
         ViewModel.ViewModel VM;
+        int saveMode { get; set; } = 1;// 1 - сохранение записей на редактирование, 2 - сохранение новых записей 
         public WindowEditAgent(ViewModel.ViewModel VM)
         {
             InitializeComponent();
@@ -30,9 +31,48 @@ namespace DemoExam.windows
 
         private void Button_Save_Click(object sender, RoutedEventArgs e)
         {
-            VM.DataBase.SaveChanges();
-            MessageBox.Show("Изменения успешно сохранены");
-            this.Close();
+            switch (saveMode)
+            {
+                case 1:
+                    try
+                    {
+                        VM.DataBase.SaveChanges();
+                        MessageBox.Show("Изменения успешно сохранены");
+                        this.Close();
+                    }
+                    catch
+                    {
+                        MessageBox.Show("Произошла ошибка, проверьте корректность введенных данных");
+                    }
+                    break;
+                case 2:
+                    try
+                    {
+                        VM.DataBase.Agent.Add(VM.SelectedAgent);
+                        VM.DataBase.SaveChanges();
+                        MessageBox.Show("Пользователь успешно добавлен!");
+                        this.Close();
+                    }
+                    catch
+                    {
+                        MessageBox.Show("Произошла ошибка, проверьте корректность введенных данных");
+                    }
+                    break;
+            }
+           
+        }
+
+        private void Window_Loaded(object sender, RoutedEventArgs e)
+        {
+            if (VM.SelectedAgent.AgentType == null)
+            {
+                saveMode = 2;
+            }
+            else
+            {
+                saveMode = 1;
+            }
+            
         }
     }
 }