add ComboBox and ListBox

This commit is contained in:
Никита Онянов 2025-03-11 12:08:02 +03:00
parent b617f2e014
commit 4f43df675b
24 changed files with 123 additions and 109 deletions

View File

@ -4,11 +4,20 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="demo_blagodat.ChangeServices" x:Class="demo_blagodat.ChangeServices"
xmlns:vm="using:demo_blagodat.Models"
Title="Изменить услугу"> Title="Изменить услугу">
<ScrollViewer> <ScrollViewer>
<StackPanel Spacing="15" Orientation="Vertical"> <StackPanel Spacing="15" Orientation="Vertical">
<TextBlock Text="Введите код сервиса:"/> <TextBlock Text="Выберите Сервис"/>
<TextBox x:Name="Name"/> <ComboBox Name="ListServices" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="vm:Service">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ServiceName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<StackPanel Spacing="15" HorizontalAlignment="Center" Orientation="Horizontal"> <StackPanel Spacing="15" HorizontalAlignment="Center" Orientation="Horizontal">

View File

@ -5,90 +5,85 @@ using Avalonia.Markup.Xaml;
using demo_blagodat.Models; using demo_blagodat.Models;
using System.Linq; using System.Linq;
using System; using System;
using System.Collections.ObjectModel;
namespace demo_blagodat; namespace demo_blagodat;
public partial class ChangeServices : Window public partial class ChangeServices : Window
{ {
public Employee User; public Employee User;
static User11Context db = new User11Context();
ObservableCollection<Service> ServiceitemSource = new ObservableCollection<Service>(db.Services);
public ChangeServices(Employee user) public ChangeServices(Employee user)
{ {
InitializeComponent(); InitializeComponent();
User = user; User = user;
ListServices.ItemsSource = ServiceitemSource;
} }
private void ComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if (sender != null) { }
}
private void changeClient(object sender, RoutedEventArgs e) private void changeClient(object sender, RoutedEventArgs e)
{ {
try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé
{ {
// Îáîçíà÷àåì êîíêðåòíîãî ïîëüçîâàòåëÿ
string? name = Name.Text;
// Ñîáèðàåì âñå äàííûå ñ ýêðàíà // Ñîáèðàåì âñå äàííûå ñ ýêðàíà
string? newname = TextNewName.Text; string? newname = TextNewName.Text;
string? newcode = TextNewCode.Text; string? newcode = TextNewCode.Text;
decimal? newprice = Math.Round(decimal.TryParse(TextNewPrice.Text.Replace(".", ","), out decimal resalt) ? resalt : -1 , 2); decimal? newprice = Math.Round(decimal.TryParse(TextNewPrice.Text.Replace(".", ","), out decimal resalt) ? resalt : -1 , 2);
if (name != "") if (ListServices.SelectedItem != null)
{ {
using (User11Context db = new User11Context()) // Ïîäêëþ÷åíèå ê ÁÄ Service service = db.Services.Where(it => it == ListServices.SelectedItem).FirstOrDefault();
if (TextNewName.Text != "")
{ {
Service? service = db.Services.Where(it => it.ServiceName == name).FirstOrDefault(); service.ServiceName = newname; // Ìåíÿåì ïàðàìåíò "Íàçâàíèå"
if (service != null)
{
if (TextNewName.Text != "")
{
service.ServiceName = newname; // Ìåíÿåì ïàðàìåíò "Íàçâàíèå"
db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ
// Ñîîáùåíèå // Ñîîáùåíèå
MesAdd.Text = "Äàííûå èçìåíåíû"; MesAdd.Text = "Äàííûå èçìåíåíû";
MesAddError.Text = ""; MesAddError.Text = "";
}
if (TextNewCode.Text != "")
{
service.ServiceCode = newcode; // Ìåíÿåì ïàðàìåíò "Êîä"
db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ
// Ñîîáùåíèå
MesAdd.Text = "Äàííûå èçìåíåíû";
MesAddError.Text = "";
}
if (newprice == -1)
{
// Ñîîáùåíèå
MesAdd.Text = "";
MesAddError.Text = "Íåïðàâèëüíàÿ öåíà";
}
else if (TextNewPrice.Text != "")
{
service.ServiceCostPerHour = (decimal) newprice;// Ìåíÿåì ïàðàìåíò "öåíà â ÷àñû"
db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ
// Ñîîáùåíèå
MesAdd.Text = "Äàííûå èçìåíåíû";
MesAddError.Text = "";
}
if(TextNewName.Text == "" && TextNewCode.Text == "" && TextNewPrice.Text == "")
{
// Ñîîáùåíèå
MesAdd.Text = "";
MesAddError.Text = "Ââåäèòå íîâûå äàííûå";
}
check(service);
}
else
{
MesAdd.Text = "";
MesAddError.Text = "Òàêîãî ñåðâèñà íåò";
}
} }
if (TextNewCode.Text != "")
{
service.ServiceCode = newcode; // Ìåíÿåì ïàðàìåíò "Êîä"
db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ
// Ñîîáùåíèå
MesAdd.Text = "Äàííûå èçìåíåíû";
MesAddError.Text = "";
}
if (newprice == -1)
{
// Ñîîáùåíèå
MesAdd.Text = "";
MesAddError.Text = "Íåïðàâèëüíàÿ öåíà";
}
else if (TextNewPrice.Text != "")
{
service.ServiceCostPerHour = (decimal)newprice;// Ìåíÿåì ïàðàìåíò "öåíà â ÷àñû"
db.SaveChanges(); // Ñîõðàíÿåì èçìåíåíèÿ
// Ñîîáùåíèå
MesAdd.Text = "Äàííûå èçìåíåíû";
MesAddError.Text = "";
}
if (TextNewName.Text == "" && TextNewCode.Text == "" && TextNewPrice.Text == "")
{
// Ñîîáùåíèå
MesAdd.Text = "";
MesAddError.Text = "Ââåäèòå íîâûå äàííûå";
}
check(service);
} }
else else
{ {
@ -115,14 +110,11 @@ public partial class ChangeServices : Window
private void check(Service service) private void check(Service service)
{ {
using (User11Context db = new User11Context()) // Ïîäêëþ÷åíèå ê ÁÄ Service checkService = db.Services.Where(it => it.ServiceName == service.ServiceName).FirstOrDefault();
{
Service checkService = db.Services.Where(it => it.ServiceName == service.ServiceName).FirstOrDefault();
SelectName.Text = checkService.ServiceName; SelectName.Text = checkService.ServiceName;
SelectCode.Text = checkService.ServiceCode; SelectCode.Text = checkService.ServiceCode;
SelectPrice.Text = checkService.ServiceCostPerHour.ToString(); SelectPrice.Text = checkService.ServiceCostPerHour.ToString();
}
} }
private void ExitClik(object sender, RoutedEventArgs e) private void ExitClik(object sender, RoutedEventArgs e)

View File

@ -4,25 +4,40 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="demo_blagodat.DeleteServices" x:Class="demo_blagodat.DeleteServices"
xmlns:vm="using:demo_blagodat.Models"
Title="Удалить Сервис"> Title="Удалить Сервис">
<ScrollViewer> <ScrollViewer>
<StackPanel Orientation="Vertical"> <StackPanel Orientation="Vertical">
<StackPanel Spacing="15" HorizontalAlignment="Center" Orientation="Vertical"> <StackPanel Spacing="15" HorizontalAlignment="Center" Orientation="Vertical">
<StackPanel Orientation="Vertical"> <Border Background="Red"
<TextBlock Text="Введите название"/> CornerRadius="10"
<TextBox Width="300" x:Name="TextName"/> Padding="20"
</StackPanel> Margin="20"
Width="400"
<StackPanel Orientation="Vertical"> Height="300"
<TextBlock Text="Введите код услуги"/> >
<TextBox Width="300" x:Name="TextCode"/> <ScrollViewer>
</StackPanel> <ListBox Name="ListServices" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="vm:Service">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ServiceName}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding ServiceCode}"/>
<TextBlock Text=" : "/>
<TextBlock Text="{Binding ServiceCostPerHour}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Border>
</StackPanel> </StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Vertical" VerticalAlignment="Center"> <StackPanel HorizontalAlignment="Center" Orientation="Vertical" VerticalAlignment="Center">
<Button Content="Удвлить Сервис" Click="deleteclient"/> <Button Content="Удалить Сервис" Click="deleteService"/>
<Button Content="Вернуться назад" Click="ExitClik"/> <Button Content="Вернуться назад" Click="ExitClik"/>
</StackPanel> </StackPanel>
<StackPanel HorizontalAlignment="Center" Orientation="Vertical"> <StackPanel HorizontalAlignment="Center" Orientation="Vertical">

View File

@ -5,58 +5,56 @@ using Avalonia.Markup.Xaml;
using demo_blagodat.Models; using demo_blagodat.Models;
using System.Linq; using System.Linq;
using System; using System;
using System.Collections.ObjectModel;
namespace demo_blagodat; namespace demo_blagodat;
public partial class DeleteServices : Window public partial class DeleteServices : Window
{ {
public Employee User; public Employee User;
static User11Context db = new User11Context();
ObservableCollection<Service> ServicesitemSource = new ObservableCollection<Service>(db.Services);
public DeleteServices(Employee user) public DeleteServices(Employee user)
{ {
InitializeComponent(); InitializeComponent();
User = user; User = user;
ListServices.ItemsSource = ServicesitemSource;
} }
private void deleteclient(object sender, RoutedEventArgs e) private void ListBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
if(sender != null) { }
}
private void deleteService(object sender, RoutedEventArgs e)
{ {
try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé try // Êîíñòðóêöèÿ äëÿ îáðàáîòêè èñêëþ÷åíèé
{ {
using (User11Context db = new User11Context()) // Ïîäêëþ÷åíèå ê ÁÄ if (ListServices.SelectedItem != null) // Ïðîâåðÿåì íàëè÷èå âàæíûõ äàííûõ
{ {
// Ñîáèðàåì äàííûå ñ ýêðàíà Service service = db.Services.Where(it => it == ListServices.SelectedItem).FirstOrDefault(); // ñîçäà¸ì ýêçåìïëÿð êëàññà Service
string? name = TextName.Text;
string? code = TextCode.Text;
if (name != null) // Ïðîâåðÿåì íàëè÷èå âàæíûõ äàííûõ // Óäàëÿåì ïîëüçîâàòåëÿ èç ÁÄ
{ db.Services.Remove(service);
Service? service = db.Services.Where(it => it.ServiceName == name).FirstOrDefault(); // ñîçäà¸ì ýêçåìïëÿð êëàññà Service
if (service != null) // Ñîõðàíÿåì èçìåíåíèÿ ÁÄ
{ db.SaveChanges();
// Óäàëÿåì ïîëüçîâàòåëÿ èç ÁÄ
db.Services.Remove(service);
// Ñîõðàíÿåì èçìåíåíèÿ ÁÄ // Óäàëÿåì èç ñïèñêà
db.SaveChanges(); ServicesitemSource.Remove(ListServices.SelectedItem as Service);
// Ìåíÿåì ñîîáùåíèå // Ìåíÿåì ñîîáùåíèå
MesDelError.Text = ""; MesDelError.Text = "";
MesDel.Text = "Ñåðâèñ óäàë¸í"; MesDel.Text = "Ñåðâèñ óäàë¸í";
}
else
{
MesDel.Text = "Òàêîãî ñåðâèñà íåò";
}
// Ñòåðàåì âñå äàííûå // Ñòåðàåì âñå äàííûå
TextName.Text = ""; ListServices.SelectedItem = null;
} }
else else
{ {
// Ìåíÿåì ñîîáùåíèå // Ìåíÿåì ñîîáùåíèå
MesDel.Text = ""; MesDel.Text = "";
MesDelError.Text = "Íóæíî äîáàâèòü íàçâàíèå èëè êîä!"; MesDelError.Text = "Íóæíî âûáðàòü ñåðâèñ";
}
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("demo_blagodat")] [assembly: System.Reflection.AssemblyCompanyAttribute("demo_blagodat")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f63e4d41bce29f31e8cca027a20f1c4f0ddbad69")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b617f2e014bf246ac7062520a6d4188404b9fb0d")]
[assembly: System.Reflection.AssemblyProductAttribute("demo_blagodat")] [assembly: System.Reflection.AssemblyProductAttribute("demo_blagodat")]
[assembly: System.Reflection.AssemblyTitleAttribute("demo_blagodat")] [assembly: System.Reflection.AssemblyTitleAttribute("demo_blagodat")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
d63b72aa80eb2f6a00640f99908a69b82a4fdfcb728d063234bb70a027739d4a 0f35feb508f4935218343a3a6854920d6f75ce11fafc95b97d8ed014f11dd19f