Jelajahi Sumber

Рассмотрели еще все методы подключения VM и исправили недочеты с извлечением информации из базы

fly9024 1 bulan lalu
induk
melakukan
dac1a37754

+ 12 - 16
AvaloniaApplication2/ViewModels/MainWindowViewModel.cs

@@ -11,7 +11,7 @@ namespace AvaloniaApplication2.ViewModels
 {
     public partial class MainWindowViewModel : ViewModelBase
     {
-        TestBase41pContext db = new TestBase41pContext();
+       
 
         
         
@@ -19,25 +19,20 @@ namespace AvaloniaApplication2.ViewModels
         [ObservableProperty] string login;
         [ObservableProperty] string password;
         [ObservableProperty] string info;
-        [ObservableProperty] Logined? currentUser;
-        [ObservableProperty] List<Gender> gendersList;
-        
+        [ObservableProperty] Logined? currentUser; 
+        [ObservableProperty] Page2ViewModel page2VM;
         [ObservableProperty] UserControl page = new Page1();
-        
+
+
+        public static MainWindowViewModel Self;
         public MainWindowViewModel()
         {
-            gendersList = db.Genders.ToList();
+            Self = this;
         }
        
-        public DateTimeOffset DateTimeOffset 
-        { 
-            get=>new DateTimeOffset((DateTime)currentUser.User.BirthDate,TimeSpan.Zero); 
-            set => currentUser.User.BirthDate = new DateTime(value.Year,value.Month,value.Day); 
-        }
-
-        public void GoBack() => Page = new Page1();
+       
 
-        public void Save() => db.SaveChanges();
+     
 
 
         public void Enter()
@@ -48,9 +43,10 @@ namespace AvaloniaApplication2.ViewModels
                 switch (CurrentUser.IdRole)
                 {
                     case 1:
-                        Page = new PageStartAdmin(db.Users.Include(x=>x.IdUserNavigation.IdRoleNavigation).ToList());
+                        Page = new PageStartAdmin();
                         break;
                     case 2:
+                        page2VM = new Page2ViewModel(currentUser.User.IdUser);
                         Page = new Page2();
                         break;
                 }                   
@@ -63,6 +59,6 @@ namespace AvaloniaApplication2.ViewModels
         }
 
 
-
+       // public void EditUser(int id) => Page = new Page2();
     }
 }

+ 31 - 0
AvaloniaApplication2/ViewModels/Page2ViewModel.cs

@@ -0,0 +1,31 @@
+using AvaloniaApplication2.Models;
+using CommunityToolkit.Mvvm.ComponentModel;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AvaloniaApplication2.ViewModels
+{
+   public partial class Page2ViewModel : ViewModelBase
+    {
+        [ObservableProperty] User editedUser;
+        [ObservableProperty] List<Gender> gendersList;
+        public Page2ViewModel(int id) 
+        {
+            GendersList = db.Genders.ToList();
+            editedUser = db.Users.Include(x=>x.IdGenderNavigation).FirstOrDefault(x=>x.IdUser == id);            
+        }
+        public DateTimeOffset DateTimeOffset
+        {
+            get => new DateTimeOffset((DateTime)EditedUser.BirthDate, TimeSpan.Zero);
+            set => EditedUser.BirthDate = new DateTime(value.Year, value.Month, value.Day);
+        }
+
+        public void GoBack() => MainWindowViewModel.Self.Page = new Page1();
+
+        public void Save() => db.SaveChanges();
+    }
+}

+ 9 - 3
AvaloniaApplication2/ViewModels/PageStartAdminViewModel.cs

@@ -19,13 +19,13 @@ namespace AvaloniaApplication2.ViewModels
         [ObservableProperty] string textFind;
         [ObservableProperty] List<Gender> genders;
         [ObservableProperty] Gender filteredGender;
-      public PageStartAdminViewModel(List<User> users) 
+      public PageStartAdminViewModel() 
         {
-            this.users = users;
+            users = db.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).ToList();
             genders =
             [
                 new Gender() { Gender1 = "Сбросить фильтр", IdGender = 0 },
-                .. users.Select(x => x.IdGenderNavigation).Distinct().OrderBy(x=>x.IdGender).ToList()
+                .. db.Genders.ToList()
             ];
             
             users0 = users;
@@ -57,5 +57,11 @@ namespace AvaloniaApplication2.ViewModels
             if (filteredGender!=null && filteredGender.IdGender !=0) Users = Users.Where(x => x.IdGenderNavigation == filteredGender).ToList();
         }
 
+        public void Press(int id)
+        {
+            MainWindowViewModel.Self.Page2VM = new Page2ViewModel(id);
+            MainWindowViewModel.Self.Page = new Page2();
+        }
+
     }
 }

+ 3 - 1
AvaloniaApplication2/ViewModels/ViewModelBase.cs

@@ -1,8 +1,10 @@
-using CommunityToolkit.Mvvm.ComponentModel;
+using AvaloniaApplication2.Models;
+using CommunityToolkit.Mvvm.ComponentModel;
 
 namespace AvaloniaApplication2.ViewModels
 {
     public class ViewModelBase : ObservableObject
     {
+        public TestBase41pContext db = new TestBase41pContext();
     }
 }

+ 7 - 8
AvaloniaApplication2/Views/Page2.axaml

@@ -4,22 +4,21 @@
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="AvaloniaApplication2.Page2"
-			  xmlns:vm="using:AvaloniaApplication2.ViewModels"
+			 xmlns:vm="using:AvaloniaApplication2.ViewModels"
 			 x:DataType="vm:MainWindowViewModel">
 	<StackPanel>
-		<TextBox Text="{Binding CurrentUser.User.Name}"/>
-		<TextBox Text="{Binding CurrentUser.User.BirthDate}"/>		
-		<DatePicker SelectedDate="{Binding DateTimeOffset}"/>
-		
-		<ComboBox ItemsSource="{Binding GendersList}" SelectedItem="{Binding CurrentUser.User.IdGenderNavigation}">
+		<TextBox Text="{Binding Page2VM.EditedUser.Name}"/>
+		<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>		
+		<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>
+		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
 			<ComboBox.ItemTemplate>
 				<DataTemplate>
 					<TextBlock Text="{Binding Gender1}"/>
 				</DataTemplate>
 			</ComboBox.ItemTemplate>
 		</ComboBox>
-		<Button Content="Сохранить изменения" Command="{Binding Save}"/>
-		<Button Content="Развойти обратно" Command="{Binding GoBack}"/>
+		<Button Content="Сохранить изменения" Command="{Binding Page2VM.Save}"/>
+		<Button Content="Развойти обратно" Command="{Binding Page2VM.GoBack}"/>
 	</StackPanel>
 	
 </UserControl>

+ 1 - 0
AvaloniaApplication2/Views/PageStartAdmin.axaml

@@ -32,6 +32,7 @@
 							<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
 							<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
 							<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
+							<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}"/>
 						</StackPanel>
 					</DataTemplate>
 				</ListBox.ItemTemplate>

+ 2 - 2
AvaloniaApplication2/Views/PageStartAdmin.axaml.cs

@@ -9,9 +9,9 @@ namespace AvaloniaApplication2;
 
 public partial class PageStartAdmin : UserControl
 {
-    public PageStartAdmin(List<User> users)
+    public PageStartAdmin()
     {
         InitializeComponent();
-        DataContext = new PageStartAdminViewModel(users);
+        DataContext = new PageStartAdminViewModel();
     }
 }