Browse Source

add and remove properties

Бобарикке 2 weeks ago
parent
commit
89706f1225

+ 17 - 2
AvaloniaApplication1/ViewModels/AddSmeshViewModel.cs

@@ -15,6 +15,7 @@ using System.Runtime.CompilerServices;
 using System.Windows.Input;
 using System.Collections.ObjectModel;
 using Avalonia.Threading;
+using Microsoft.EntityFrameworkCore;
 
 namespace AvaloniaApplication1.ViewModels
 {
@@ -38,7 +39,7 @@ namespace AvaloniaApplication1.ViewModels
         public string Animal { get => animal; set => this.RaiseAndSetIfChanged(ref animal, value); }
         public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
         public List<Property> Property { get => property; set => this.RaiseAndSetIfChanged(ref property, value); }
-        public List<Property> Property2 { get => property; set => this.RaiseAndSetIfChanged(ref property, value); }
+        public List<Property> Property2 { get => property2; set => this.RaiseAndSetIfChanged(ref property2, value); }
         public List<Property> SmeshProperty { get => smeshProperty; set => this.RaiseAndSetIfChanged(ref smeshProperty, value); }
         public Gender SelectedGender { get => selectedGender; set => this.RaiseAndSetIfChanged(ref selectedGender, value); }
         public Property SelectedProperty { get => selectedProperty; set  { selectedProperty = value; AddNewItem(); } }
@@ -51,7 +52,7 @@ namespace AvaloniaApplication1.ViewModels
         public AddSmeshViewModel()
 		{
             property = myConnection.Properties.ToList();
-            property2 = property.ToList();
+            property2 = myConnection.Properties.ToList();
             selectedProperty = property.FirstOrDefault();
             genders = myConnection.Genders.ToList();
 		}
@@ -103,6 +104,20 @@ namespace AvaloniaApplication1.ViewModels
                 }
             });
         }
+        public async void DeleteItem(int id)
+        {
+            var newItem = await myConnection.Properties.FirstOrDefaultAsync(x=> x.Id ==id);
+            await Dispatcher.UIThread.InvokeAsync(() =>
+            {
+                if (newItem != null)
+                {
+                    Items.Remove(newItem);
+                    Property = Property2;
+                    Property = Property.Except(Items).ToList();
+                
+            }
+            });
+        }
         public event PropertyChangedEventHandler PropertyChanged;
         protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
         {

+ 4 - 4
AvaloniaApplication1/ViewModels/UpdateSmesharikViewModel.cs

@@ -23,7 +23,7 @@ namespace AvaloniaApplication1.ViewModels
 		Smeshariki smesh;
 		List<Gender> genders;
         Bitmap imageSmesh;
-        public ICommand DeleteItem { get; }
+        public ICommand DeleteItemCommand { get; }
         public ObservableCollection<Property> Properties { get; } = new ObservableCollection<Property>();
         public Smeshariki Smesh { get => smesh; set => this.RaiseAndSetIfChanged(ref smesh, value); }
         public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
@@ -67,14 +67,14 @@ namespace AvaloniaApplication1.ViewModels
 
         public void Back() => MainWindowViewModel.Self.Uc = new AdminPage();
 
-        private async void DeleteProperty(int id)
+        public async void DeleteProperty(int id)
         {
-            var newItem = Properties.FirstOrDefault(X => X.Id == id);
+            var newItem = await myConnection.Properties.FirstOrDefaultAsync(x=> x.Id == id);
             await Dispatcher.UIThread.InvokeAsync(() =>
             {
                 if (newItem != null)
                 {
-                    Properties.Remove(newItem);
+                    Smesh.IdProperties.Remove(newItem);
                 }
             });
         }

+ 4 - 1
AvaloniaApplication1/Views/AddSmesh.axaml

@@ -38,7 +38,10 @@
 				<ListBox ItemsSource="{Binding Items}" Width="150" Height="300">
 					<ListBox.ItemTemplate>
 						<DataTemplate>
-							<TextBlock Text="{Binding Title}" Foreground="Black"/>
+							<StackPanel Orientation="Horizontal">
+								<Button Content="-" Command="{Binding $parent[UserControl].((vm:AddSmeshViewModel)DataContext).DeleteItem}" CommandParameter="{Binding Id}"/>
+								<TextBlock Text="{Binding Title}" Foreground="Black"/>
+							</StackPanel>
 						</DataTemplate>
 					</ListBox.ItemTemplate>
 				</ListBox>