Selaa lähdekoodia

Создал VM для страницы администратора и доделал сортировки и фильтры

fly9024 10 kuukautta sitten
vanhempi
commit
35e6801904

+ 34 - 72
AvaloniaApplication5/ViewModels/MainWindowViewModel.cs

@@ -12,83 +12,16 @@ namespace AvaloniaApplication5.ViewModels
 {
     public class MainWindowViewModel : ViewModelBase
     {
-        _1234Context DBConnect = new _1234Context();
+       static  _1234Context DBConnect = new _1234Context();
 
         private UserControl uc = new PageLogin();
-        public UserControl UC { get => uc;
-            set => this.RaiseAndSetIfChanged(ref uc, value); }
+        public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
 
-              
+
+        #region VMLogin
 
         PageLoginViewModel pageLoginVM = new PageLoginViewModel();
         public PageLoginViewModel PageLoginVM { get => pageLoginVM; set => pageLoginVM = value; }
-        public PageUserCabinetViewModel PageUCabinetVM { get => pageUCabinetVM; set => pageUCabinetVM = value; }
-
-        private PageUserCabinetViewModel pageUCabinetVM;
-        #region VMadmin
-        
-        UserControl ucAdmin;
-        public UserControl UCAdmin
-        {
-            get => ucAdmin;
-            set => this.RaiseAndSetIfChanged(ref ucAdmin, value);
-        }
-        
-        public MainWindowViewModel()
-        {
-            userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList();
-        }
-        List<Logintable> userList;
-        public List<Logintable> UserList { get => userList; set =>this.RaiseAndSetIfChanged(ref userList,value); }
-        public void EditUser(int id)
-        {
-            PageUCabinetVM = id==0?  new PageUserCabinetViewModel(DBConnect):new PageUserCabinetViewModel(DBConnect, id);
-            UCAdmin = new PageUserCabinet();
-        }
-        public List<string> OrdersName => new List<string>() { "По возрастанию", "По убыванию" };
-        string orderByName = "По возрастанию";
-        public string OrderByName { 
-            get => orderByName; 
-        set
-            {
-                orderByName = value;
-                switch (value)
-                {
-                    case "По возрастанию":                        
-                        UserList = UserList.OrderBy(x => x.User.Name).ToList();
-                        break;
-                    case "По убыванию":
-                        UserList = UserList.OrderByDescending(x => x.User.Name).ToList();
-                        break;
-                }                
-            }
-        }
-
-        public List<string> Genders//список полов для фильтра
-        {
-            get
-            {
-                List<string> newlist = new List<string>() {"Любой"};
-                newlist.AddRange(DBConnect.Genders.Select(x => x.Gender1).ToList());
-                return newlist;
-            }
-        }
-
-        public string FilterByGender { get => filterByGender;
-            set
-            {
-               
-                if (!(value =="Любой" || value == null))
-                UserList = UserList.Where(x => x.User.IdGenderNavigation.Gender1 == value).ToList();        
-            } 
-        }
-
-        string filterByGender = "Любой";
-
-
-
-        #endregion
-
         public void LoadPageList()
         {
             Logintable? currentUser = DBConnect.Logintables.FirstOrDefault(x => x.Login == PageLoginVM.Login && x.Password == PageLoginVM.Password);
@@ -100,12 +33,41 @@ namespace AvaloniaApplication5.ViewModels
                         UC = new PageUserList();
                         break;
                     case 2:
-                        PageUCabinetVM = new PageUserCabinetViewModel(DBConnect,currentUser.Id);
+                        PageUCabinetVM = new PageUserCabinetViewModel(DBConnect, currentUser.Id);
                         UC = new PageUserCabinet();
                         break;
                 }
             }
             else PageLoginVM.Message = "Пользователь отсутствует в системе, либо указанные данные введены неверно";
         }
+        #endregion
+
+        #region VMUserCabinet
+
+        private PageUserCabinetViewModel pageUCabinetVM;
+        public PageUserCabinetViewModel PageUCabinetVM { get => pageUCabinetVM; set => pageUCabinetVM = value; }
+        #endregion
+
+
+
+        #region VMadmin
+        private PageUserListViewModel pageUserListViewVM = new PageUserListViewModel(DBConnect);
+        public PageUserListViewModel PageUserListViewVM { get => pageUserListViewVM; set => pageUserListViewVM = value; }
+
+        UserControl ucAdmin;
+        public UserControl UCAdmin
+        {
+            get => ucAdmin;
+            set => this.RaiseAndSetIfChanged(ref ucAdmin, value);
+        }
+
+        public void EditUser(int id)
+        {
+            PageUCabinetVM = id == 0 ? new PageUserCabinetViewModel(DBConnect) : new PageUserCabinetViewModel(DBConnect, id);
+            UCAdmin = new PageUserCabinet();
+        }
+        #endregion
+
+
     }
 }

+ 87 - 0
AvaloniaApplication5/ViewModels/PageUserListViewModel.cs

@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+using Avalonia.Controls;
+using AvaloniaApplication5.Models;
+using AvaloniaApplication5.Views;
+using Microsoft.EntityFrameworkCore;
+using System.Linq;
+using ReactiveUI;
+
+namespace AvaloniaApplication5.ViewModels
+{
+	public class PageUserListViewModel : ReactiveObject
+	{
+        _1234Context DBConnect;
+                
+        public PageUserListViewModel(_1234Context dBConnect)
+        {
+            this.DBConnect = dBConnect;
+            userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList();
+        }
+        List<Logintable> userList;
+        public List<Logintable> UserList { get => userList; set => this.RaiseAndSetIfChanged(ref userList, value); }
+       
+        public List<string> OrdersName => new List<string>() { "Ïî âîçðàñòàíèþ", "Ïî óáûâàíèþ" };
+        string orderByName = "Ïî âîçðàñòàíèþ";
+        public string OrderByName
+        {
+            get => orderByName;
+            set
+            {
+                orderByName = value;
+                AllFilters();
+            }
+        }
+
+        public List<string> Genders//ñïèñîê ïîëîâ äëÿ ôèëüòðà
+        {
+            get
+            {
+                List<string> newlist = new List<string>() { "Ëþáîé" };
+                newlist.AddRange(DBConnect.Genders.Select(x => x.Gender1).ToList());
+                return newlist;
+            }
+        }
+        string filterByGender = "Ëþáîé";
+        public string FilterByGender
+        {
+            get => filterByGender;
+            set
+            {
+                filterByGender = value;
+                AllFilters();                
+            }
+        }
+
+        string namefind;
+        public string Namefind { get => namefind; set { namefind = value; AllFilters(); } }
+        
+        void AllFilters()
+        {
+            //âñåãäà ïåðåä ñîðòèðîâêîé ïîëó÷àåì íîâûé ëèñò
+            userList = DBConnect.Logintables.Include(x => x.IdRoleNavigation).Include(x => x.User.IdGenderNavigation).ToList();
+            //ïîèñê ïî èìåíè
+            if (!(namefind == "" || namefind == null))
+                UserList = UserList.Where(x => x.User.Name.Contains(namefind)).ToList();
+
+
+            //ñîðòèðîâêà ïî âîçðàñòàíèþ èìåíè
+            switch (orderByName)
+            {
+                case "Ïî âîçðàñòàíèþ":
+                    UserList = UserList.OrderBy(x => x.User.Name).ToList();
+                    break;
+                case "Ïî óáûâàíèþ":
+                    UserList = UserList.OrderByDescending(x => x.User.Name).ToList();
+                    break;
+            }
+
+            //ôèëüòð ïî ïîëó
+            if (!(filterByGender == "Ëþáîé" || filterByGender == null))
+                UserList = UserList.Where(x => x.User.IdGenderNavigation.Gender1 == filterByGender).ToList();
+
+           
+        }
+
+    }
+}

+ 6 - 5
AvaloniaApplication5/Views/PageUserList.axaml

@@ -8,18 +8,19 @@
 			 x:Class="AvaloniaApplication5.Views.PageUserList">
 	<Grid ColumnDefinitions="*,*" RowDefinitions="50,*">
 		<StackPanel Orientation="Horizontal" Grid.Row="0">
-			<Button Content="Добавить пользователя" Command="{Binding $parent[Window].((vm:MainWindowViewModel)DataContext).EditUser}" CommandParameter="0"/>
+			<!--<Button Content="Добавить пользователя" Command="{Binding $parent[Window].((vm:MainWindowViewModel)DataContext).EditUser}" CommandParameter="0"/>-->
+			<Button Content="Добавить пользователя" Command="{Binding EditUser}" CommandParameter="0"/>
 			<StackPanel>
 				<TextBlock Text="Сортировка по имени"/>
-				<ComboBox ItemsSource="{Binding OrdersName}" SelectedValue="{Binding OrderByName}"/>
+				<ComboBox ItemsSource="{Binding PageUserListViewVM.OrdersName}" SelectedValue="{Binding PageUserListViewVM.OrderByName}"/>
 			</StackPanel>
 			<StackPanel>
 				<TextBlock Text="Фильтр по полу"/>
-				<ComboBox ItemsSource="{Binding Genders}" SelectedValue="{Binding FilterByGender}"/>
+				<ComboBox ItemsSource="{Binding PageUserListViewVM.Genders}" SelectedValue="{Binding PageUserListViewVM.FilterByGender}"/>
 			</StackPanel>
 			<StackPanel>
 				<TextBlock Text="Поиск по имени"/>
-				<TextBox />
+				<TextBox Text="{Binding PageUserListViewVM.Namefind}"/>
 			</StackPanel>
 			
 			
@@ -27,7 +28,7 @@
 				
 			
 		</StackPanel>
-		<ListBox Grid.Column="0" Grid.Row="1" ItemsSource="{Binding UserList}">
+		<ListBox Grid.Column="0" Grid.Row="1" ItemsSource="{Binding PageUserListViewVM.UserList}">
 			<ListBox.ItemTemplate>
 				<DataTemplate>
 					<StackPanel>