فهرست منبع

Более-менее нормально сделанные совместные фильтры

fly9024 1 سال پیش
والد
کامیت
e55078518c
4فایلهای تغییر یافته به همراه76 افزوده شده و 20 حذف شده
  1. 1 0
      MyWpfApp/MyWpfApp.csproj
  2. 3 3
      MyWpfApp/Pages/PageUserList.xaml
  3. 7 17
      MyWpfApp/Pages/PageUserList.xaml.cs
  4. 65 0
      MyWpfApp/models/UserListModel.cs

+ 1 - 0
MyWpfApp/MyWpfApp.csproj

@@ -72,6 +72,7 @@
       <DesignTime>True</DesignTime>
       <DependentUpon>Model1.tt</DependentUpon>
     </Compile>
+    <Compile Include="models\UserListModel.cs" />
     <Compile Include="Pages\PageForBindings.xaml.cs">
       <DependentUpon>PageForBindings.xaml</DependentUpon>
     </Compile>

+ 3 - 3
MyWpfApp/Pages/PageUserList.xaml

@@ -34,16 +34,16 @@
         <GroupBox Header="Всякие разные фильтры">
             <StackPanel Orientation="Horizontal">
                 <GroupBox Header="Поиск по фамилии">
-                    <TextBox x:Name="txtSurnameFind" TextChanged="txtSurnameFind_TextChanged"/>
+                    <TextBox Text="{Binding Surname,Mode=TwoWay}"/>
                 </GroupBox>
                 <GroupBox Header="Фильтр по роли">
-                    <ComboBox x:Name="cbRoleFind" SelectionChanged="cbRoleFind_SelectionChanged"/>
+                    <ComboBox ItemsSource="{Binding Roles}" SelectedValuePath="ID" DisplayMemberPath="Role" SelectedValue="{Binding IdRole}"/>
                 </GroupBox>
                 <Button Content="Сбросить все фильтры" Name="btnResetFilters" Click="btnResetFilters_Click"/>
             </StackPanel>
         </GroupBox>
         
-        <ListBox x:Name="LB1" ItemTemplate="{StaticResource DT1}"/>
+        <ListBox x:Name="LB1" ItemTemplate="{StaticResource DT1}" ItemsSource="{Binding Users,Mode=TwoWay}"/>
         <Button Content="Добавить нового пользователя" Name="BtnAddUser" Click="BtnAddUser_Click"/>
     </StackPanel>
 </Page>

+ 7 - 17
MyWpfApp/Pages/PageUserList.xaml.cs

@@ -21,15 +21,11 @@ namespace MyWpfApp.Pages
     /// </summary>
     public partial class PageUserList : Page
     {
-        List<UserTable> users = Globals.MyGlobalConnection.UserTable.ToList();
+        UserListModel users = new UserListModel();
         public PageUserList()
         {
-            InitializeComponent();
-            LB1.ItemsSource = users;
-            cbRoleFind.ItemsSource = Globals.MyGlobalConnection.Roles.ToList();
-            cbRoleFind.SelectedValuePath = "ID";
-            cbRoleFind.DisplayMemberPath = "Role";
-
+            InitializeComponent();           
+            DataContext = users;          
         }
 
        
@@ -50,19 +46,13 @@ namespace MyWpfApp.Pages
             Globals.MyGlobalFrame.Navigate(new PageUserInfo());
         }
 
-        private void txtSurnameFind_TextChanged(object sender, TextChangedEventArgs e)
-        {
-            LB1.ItemsSource = users.Where(x=>x.Surname.StartsWith(txtSurnameFind.Text)).ToList();    
-        }
-
-        private void cbRoleFind_SelectionChanged(object sender, SelectionChangedEventArgs e)
-        {
-            LB1.ItemsSource = users.Where(x=>x.LoginedTable.IDRole == (int)cbRoleFind.SelectedValue).ToList();
-        }
+       
+        
 
         private void btnResetFilters_Click(object sender, RoutedEventArgs e)
         {
-            LB1.ItemsSource = users;
+            users.Surname = null;
+            users.IdRole = 0;
         }
     }
 }

+ 65 - 0
MyWpfApp/models/UserListModel.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.ComponentModel;
+using System.Windows.Controls;
+using System.Windows;
+
+namespace MyWpfApp.models
+{
+    internal class UserListModel:INotifyPropertyChanged
+    {
+
+        List<UserTable> users;
+
+        List<Roles> roles;
+        int idRole;
+        string surname;
+
+        public UserListModel()
+        {
+            users = Globals.MyGlobalConnection.UserTable.ToList();
+            roles = Globals.MyGlobalConnection.Roles.ToList();
+            roles.Add(new Roles() { ID = 0, Role = "Все роли" }); 
+            idRole = 0;
+        }
+
+        public List<UserTable> Users { get=>users; set
+                {
+                    users = value;
+                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Users"));                
+                }
+            }
+
+        public List<Roles> Roles { get => roles; }
+        public int IdRole { get => idRole; 
+            set 
+                {
+                idRole = value;
+                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IdRole"));
+                AllFilters();
+                }
+        }
+
+        public string Surname { get => surname; set
+            {
+                surname = value;
+                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Surname"));
+                AllFilters();                
+            }
+        }
+
+        void AllFilters()
+        {
+            List<UserTable> userFiltered = Globals.MyGlobalConnection.UserTable.ToList();
+            if (idRole !=0) userFiltered = userFiltered.Where(x => x.LoginedTable.IDRole == idRole).ToList();
+            if (surname != null) userFiltered = userFiltered.Where(x => x.Surname.StartsWith(surname)).ToList();
+            Users = userFiltered;
+        }
+
+
+        public event PropertyChangedEventHandler PropertyChanged;
+    }
+}