Преглед на файлове

пыталась сделать добавление и редактирование

Priquo преди 2 години
родител
ревизия
d90a61e28c

+ 4 - 3
DemoexamUser11/pages/AgentsList.xaml

@@ -10,7 +10,7 @@
     <Page.Resources>
         <DataTemplate x:Key="listItemTemplate">
             <Border BorderBrush="Black" BorderThickness="1" Height="120">
-                <StackPanel Orientation="Horizontal" >
+                <StackPanel Orientation="Horizontal" MouseDown="StackPanel_MouseDown">
                     <Image Source="{Binding ImagePath}" Height="120" Width="120"/>
                     <StackPanel Orientation="Vertical" Margin="5 10" Width="600">
                         <StackPanel Orientation="Horizontal">
@@ -33,11 +33,12 @@
             <RowDefinition Height="137*"/>
         </Grid.RowDefinitions>
         <StackPanel Orientation="Horizontal" Grid.Row="0">
-            <TextBox x:Name="textBoxSearch" Width="250" Height="25" Margin="10 0" TextChanged="Search"/>
-            <ComboBox x:Name="comboBoxSort" Width="150" Height="25" SelectionChanged="Filter"/>
+            <TextBox x:Name="textBoxSearch" Width="200" Height="25" Margin="10 0" TextChanged="Search"/>
+            <ComboBox x:Name="comboBoxSort" Width="100" Height="25" SelectionChanged="Filter"/>
             <Button x:Name="buttomBigger" Content="Изменить порядок" Height="25" Click="buttomBigger_Click"/>
             <ComboBox x:Name="comboBoxFilter" Width="100" Height="25" Margin="10 0" SelectionChanged="Filter"/>
             <Button x:Name="buttomChangePriority" Content="Изменить приоритет" Height="25" Click="buttomChangePriority_Click"/>
+            <Button x:Name="buttomAddAgent" Content="Добавить" Height="25" Click="buttomAddAgent_Click" Margin="10 0"/>
         </StackPanel>
         <ListBox x:Name="listBoxAgents" ItemTemplate="{StaticResource listItemTemplate}" Grid.Row="1" SelectionMode="Multiple"/>
     </Grid>

+ 20 - 0
DemoexamUser11/pages/AgentsList.xaml.cs

@@ -119,5 +119,25 @@ namespace DemoexamUser11.pages
                 listBoxAgents.Items.Refresh();
             }
         }
+        private void buttomAddAgent_Click(object sender, RoutedEventArgs e)
+        {
+            WindowEditAgent windowEditAgent = new WindowEditAgent();
+            if (Application.Current.Windows.Count < 5)
+                windowEditAgent.Show();
+            agents = BaseConnector.BaseConnect.Agent.ToList();
+            listBoxAgents.Items.Refresh();
+        }
+
+        private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
+        {
+            if (listBoxAgents.SelectedItem != null)
+            {
+                WindowEditAgent windowEditAgent = new WindowEditAgent((Agent)listBoxAgents.SelectedItem);
+                if (Application.Current.Windows.Count < 5)
+                    windowEditAgent.Show();
+                agents = BaseConnector.BaseConnect.Agent.ToList();
+                listBoxAgents.Items.Refresh();
+            }
+        }
     }
 }

+ 26 - 1
DemoexamUser11/windows/WindowEditAgent.xaml

@@ -7,6 +7,31 @@
         mc:Ignorable="d"
         Title="WindowEditAgent" Height="450" Width="800">
     <Grid>
-        
+        <StackPanel Orientation="Vertical">
+            <TextBlock Text="Наименование"/>
+            <TextBox x:Name="textBoxTitle" />
+            <TextBlock Text="Тип агента"/>
+            <ComboBox Name="comboBoxAgentType" Width="200" HorizontalAlignment="Left"/>
+            <TextBlock Text="Приоритет"/>
+            <TextBox x:Name="textBoxPriority" />
+            <TextBlock x:Name="textBlockImagePath" Text="Путь к картинке" />
+            <Button x:Name="buttonAddImage" Content="Добавить лого компании" Width="200" HorizontalAlignment="Left"/>
+            <TextBlock Text="Адрес"/>
+            <TextBox x:Name="textBoxAddres" />
+            <TextBlock Text="ИНН"/>
+            <TextBox x:Name="textBoxINN" />
+            <TextBlock Text="КПП"/>
+            <TextBox x:Name="textBoxKPP" />
+            <TextBlock Text="Имя директора"/>
+            <TextBox x:Name="textBoxNameDirector" />
+            <TextBlock Text="Телефон"/>
+            <TextBox x:Name="textBoxPhone" />
+            <TextBlock Text="Email"/>
+            <TextBox x:Name="textBoxEmail" />
+            <StackPanel Orientation="Horizontal">
+                <Button x:Name="buttonSave" Content="Сохранить" Margin="0 0 50 0" Click="buttonSave_Click"/>
+                <Button x:Name="buttonDelete" Content="Удалить" Click="buttonDelete_Click"/>
+            </StackPanel>
+        </StackPanel>
     </Grid>
 </Window>

+ 68 - 0
DemoexamUser11/windows/WindowEditAgent.xaml.cs

@@ -11,6 +11,7 @@ using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Shapes;
+using DemoexamUser11.classes;
 
 namespace DemoexamUser11.windows
 {
@@ -19,9 +20,76 @@ namespace DemoexamUser11.windows
     /// </summary>
     public partial class WindowEditAgent : Window
     {
+        bool isNewAgent = false;
+        Agent agent;
+        List<AgentType> types = BaseConnector.BaseConnect.AgentType.ToList();
         public WindowEditAgent()
         {
             InitializeComponent();
+            Title = "Добавление нового агента";
+            isNewAgent = true;
+            comboBoxAgentType.ItemsSource = types;
+            comboBoxAgentType.SelectedValuePath = "ID";
+            comboBoxAgentType.DisplayMemberPath = "Title";
+            textBlockImagePath.Text = "/images/picture.png";
+        }
+        public WindowEditAgent(Agent agent)
+        {
+            InitializeComponent();
+            Title = "Редактирование агента";
+            this.agent = agent;
+            comboBoxAgentType.ItemsSource = types;
+            comboBoxAgentType.SelectedValuePath = "ID";
+            comboBoxAgentType.DisplayMemberPath = "Title";
+            comboBoxAgentType.SelectedValue = agent.AgentTypeID;
+        }
+
+        private void buttonSave_Click(object sender, RoutedEventArgs e)
+        {
+            try
+            {
+                if (isNewAgent == true)
+                {
+                    int lastID = BaseConnector.BaseConnect.Agent.ToList().Last().ID + 1;
+                    BaseConnector.BaseConnect.Agent.Add(new Agent()
+                    {
+                        ID = lastID,
+                        Address = textBoxAddres.Text,
+                        Title = textBoxTitle.Text,
+                        AgentTypeID = Convert.ToInt32(comboBoxAgentType.SelectedValue),
+                        Priority = Convert.ToInt32(textBoxPriority.Text),
+                        Logo = textBlockImagePath.Text,
+                        INN = textBoxINN.Text,
+                        KPP = textBoxKPP.Text,
+                        DirectorName = textBoxNameDirector.Text,
+                        Phone = textBoxPhone.Text,
+                        Email = textBoxEmail.Text
+                    });
+                    BaseConnector.BaseConnect.SaveChanges();
+                    MessageBox.Show("Успешно добавлено!");
+                }                
+            }
+            catch
+            {
+                MessageBox.Show("Не удалось добавить агента!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
+            }
+        }
+
+        private void buttonDelete_Click(object sender, RoutedEventArgs e)
+        {
+            if (isNewAgent == false)
+            {
+                try
+                {
+                    BaseConnector.BaseConnect.Agent.Remove(agent);
+                    BaseConnector.BaseConnect.SaveChanges();
+                    MessageBox.Show("Успех!");
+                }
+                catch
+                {
+                    MessageBox.Show("Не удалось удалить агента!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
+                }
+            }
         }
     }
 }