· 8 years ago · Nov 27, 2017, 01:00 AM
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.ComponentModel;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using System.Windows;
9using System.Windows.Controls;
10using System.Windows.Data;
11using System.Windows.Documents;
12using System.Windows.Input;
13using System.Windows.Media;
14using System.Windows.Media.Imaging;
15using System.Windows.Navigation;
16using System.Windows.Shapes;
17
18public enum AccountType { Student, Teacher }
19
20public enum BookGenre
21{
22 ActionAndAdventure, Anthology, Art, Autobiography, Biography, Childrens,
23 Comic, Cookbook, Dictionary, Drama, Encyclopedia, Fantasy, Guide, History,
24 Horror, Math, Mystery, Poetry, Religion, Romance, Satire, Science, ScienceFiction
25}
26
27namespace Echo_Library_Software
28{
29 /// <summary>
30 /// Interaction logic for MainWindow.xaml
31 /// </summary>
32 public partial class MainWindow : Window
33 {
34 //ObservableCollection refreshes in GUI when changed.
35 public static ObservableCollection<User> users = new ObservableCollection<User>();
36 public static ObservableCollection<Book> books = new ObservableCollection<Book>();
37
38 public MainWindow()
39 {
40 InitializeComponent();
41 FillList();
42
43 //Bind listviews to lists in code.
44 UsersListView.ItemsSource = users;
45
46 //Sets up searchbar filtering system.
47 CollectionView _view = (CollectionView) CollectionViewSource.GetDefaultView(UsersListView.ItemsSource);
48 _view.Filter = UserFilter;
49 }
50
51 //Returns new index of object we are searching for.
52 private bool UserFilter(object item)
53 {
54 if (String.IsNullOrEmpty(UsersTextFilter.Text))
55 return true;
56 else
57 return ((item as User).Name.IndexOf(UsersTextFilter.Text, StringComparison.OrdinalIgnoreCase) >= 0);
58 }
59
60 //Method that is executed when the text inside the search box changes.
61 private void textFilter_TextChange(object sender, System.Windows.Controls.TextChangedEventArgs e)
62 {
63 CollectionViewSource.GetDefaultView(UsersListView.ItemsSource).Refresh();
64 }
65
66 //Opens UserCreator window.
67 private void OpenUserCreator(object sender, RoutedEventArgs e)
68 {
69 UserCreator _userCreator = new UserCreator();
70 _userCreator.Show();
71 }
72
73 //Opens UserCreator window.
74 private void OpenBookCreator(object sender, RoutedEventArgs e)
75 {
76 BookCreator _bookCreator = new BookCreator();
77 _bookCreator.Show();
78 }
79
80 private void FillList()
81 {
82 users.Add(new User()
83 {
84 Name = "Jimmy Neutron",
85 Age = 10,
86 AccountType = AccountType.Student,
87 Email = "JimmyNeutron@Ducks.net",
88 PhoneNumber = 7703856678
89 });
90 users.Add(new User()
91 {
92 Name = "Daddy",
93 Age = 70,
94 AccountType = AccountType.Teacher,
95 Email = "Daddy@sugar.com",
96 PhoneNumber = 6667859798
97 });
98 users.Add(new User()
99 {
100 Name = "Sahand Allahu",
101 Age = 32,
102 AccountType = AccountType.Student,
103 Email = "LilAllah@gaynotgay.com",
104 PhoneNumber = 7708886564
105 });
106
107 /*for (int i = 0; i < 150; i++)
108 {
109 users.Add(new User()
110 {
111 Name = "Steve" + i,
112 Age = 15,
113 AccountType = AccountType.Teacher,
114 Email = "steve" + i + "@gmail.com",
115 PhoneNumber = 6665554443 + i
116 });
117 }*/
118 }
119
120 private void RemoveUser(object sender, RoutedEventArgs e)
121 {
122 //Loop through list
123 foreach (User _user in users)
124 {
125 //Find if current item is equal to item selected in listview
126 if (_user == UsersListView.SelectedItem)
127 {
128 users.Remove(_user); //Remove item if it is
129
130 break;
131 }
132 }
133 }
134
135 //Opens dialog box showing full data of selected user.
136 private void ShowUserData(object sender, MouseButtonEventArgs e)
137 {
138 User _selectedUser = (User)UsersListView.SelectedItem;
139
140 MessageBoxResult _messageBoxResult =
141 MessageBox.Show(_selectedUser.DataToString(), "User Info");
142 }
143 }
144
145 public class User
146 {
147 public string Name;
148 public int Age;
149 public AccountType AccountType;
150 public string Email;
151 public long PhoneNumber;
152
153 //Converts all data into a string to be shown in a dialogbox.
154 public string DataToString()
155 {
156 return "Name: " + Name +
157 "\nAge: " + Age +
158 "\nAccountType: " + AccountType +
159 "\nEmail: " + Email +
160 "\nPhoneNumber: " + PhoneNumber;
161 }
162 }
163
164 public class Book
165 {
166 public string Title;
167 public string Author;
168 public BookGenre BookGenre;
169 public string ISBN;
170
171 public string DataToString()
172 {
173 return "Title: " + Title +
174 "\nAuthor: " + Author +
175 "\nBook Genre: " + BookGenre +
176 "\nISBN: " + ISBN;
177 }
178 }
179}