MainWindow.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System.ComponentModel;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Data;
  6. using System.Windows.Documents;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Navigation;
  11. using System.Windows.Shapes;
  12. using WpfApp1.model.WpfApp4.model;
  13. using WpfApp1.model;
  14. using System.IO;
  15. using System.Runtime.Serialization.Json;
  16. namespace WpfApp1
  17. {
  18. public partial class MainWindow : Window, INotifyPropertyChanged
  19. {
  20. public event PropertyChangedEventHandler PropertyChanged;
  21. private void Invalidate()
  22. {
  23. if (PropertyChanged != null)
  24. PropertyChanged(this, new PropertyChangedEventArgs("ClientList"));
  25. }
  26. public string selectedCategory = "Все категории";
  27. public ClientPrice? selectedPrice = null;
  28. private IEnumerable<Client> _ClientList;
  29. public IEnumerable<Client> ClientList
  30. {
  31. get
  32. {
  33. var res = _ClientList;
  34. res = res
  35. .Where(c => (selectedPrice == null || (c.Price >= selectedPrice.PriceFrom && c.Price < selectedPrice.PriceTo)))
  36. .Where(c => (c.Category == selectedCategory || selectedCategory == "Все категории"));
  37. if (searchFilter != "")
  38. res = res.Where(c => c.Name.IndexOf(
  39. searchFilter,
  40. StringComparison.OrdinalIgnoreCase) >= 0 || c.Category.IndexOf(
  41. searchFilter,
  42. StringComparison.OrdinalIgnoreCase) >= 0 || c.Place.IndexOf(
  43. searchFilter,
  44. StringComparison.OrdinalIgnoreCase) >= 0);
  45. if (sortAsc) res = res.OrderBy(c => c.Price);
  46. else res = res.OrderByDescending(c => c.Price);
  47. if (sortAsc) res = res.OrderBy(c => c.Price);
  48. else res = res.OrderByDescending(c => c.Price);
  49. return res;
  50. }
  51. set
  52. {
  53. _ClientList = value;
  54. }
  55. }
  56. public List<Client> Client { get; set; }
  57. public List<ClientCategory> ClientCategoryList { get; set; }
  58. public List<ClientPrice> ClientPriceList { get; set; }
  59. public MainWindow()
  60. {
  61. {
  62. InitializeComponent();
  63. DataContext = this;
  64. Globals.dataProvider = new JSONDataProvider();
  65. ClientList = Globals.dataProvider.getClient();
  66. ClientPriceList = Globals.dataProvider.getPrice().ToList();
  67. ClientCategoryList = Globals.dataProvider.getCategory().ToList();
  68. ClientCategoryList.Insert(0, new ClientCategory() { title = "Все категории" });
  69. }
  70. }
  71. public class JSONDataProvider: LocalDataProvider, IDataProvider
  72. {
  73. private List<Client> _ClientList;
  74. public JSONDataProvider()
  75. {
  76. var serializer = new DataContractJsonSerializer(typeof(Client[]));
  77. using (var sr = new StreamReader("./data.json"))
  78. {
  79. _ClientList = ((Client[])serializer.ReadObject(sr.BaseStream)).ToList();
  80. }
  81. }
  82. public IEnumerable<Client> getClient()
  83. {
  84. return _ClientList;
  85. }
  86. }
  87. private void ExitButton_Click(object sender, RoutedEventArgs e)
  88. {
  89. Application.Current.Shutdown();
  90. }
  91. private string searchFilter = "";
  92. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  93. {
  94. searchFilter = SearchFilterTextBox.Text;
  95. Invalidate();
  96. }
  97. private void ClientCategoryFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  98. {
  99. selectedCategory = (CategoryFilterComboBox.SelectedItem as ClientCategory).title;
  100. Invalidate();
  101. }
  102. private void ClientPriceFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  103. {
  104. selectedPrice = PriceFilterComboBox.SelectedItem as ClientPrice;
  105. Invalidate();
  106. }
  107. private bool sortAsc = true;
  108. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  109. {
  110. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  111. Invalidate();
  112. }
  113. private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  114. {
  115. }
  116. }
  117. interface IDataProvider
  118. {
  119. IEnumerable<Client> getClient();
  120. IEnumerable<ClientPrice> getPrice();
  121. IEnumerable<ClientCategory> getCategory();
  122. }
  123. public class LocalDataProvider : IDataProvider
  124. {
  125. public IEnumerable<ClientCategory> getCategory()
  126. {
  127. return new ClientCategory[]
  128. {
  129. new ClientCategory()
  130. {
  131. title="По России"
  132. },
  133. new ClientCategory()
  134. {
  135. title="За границей"
  136. },
  137. };
  138. }
  139. public IEnumerable<ClientPrice> getPrice()
  140. {
  141. return new ClientPrice[]
  142. {
  143. new ClientPrice()
  144. {
  145. title = "Все цены",
  146. PriceFrom = 500,
  147. PriceTo = 30000
  148. },
  149. new ClientPrice()
  150. {
  151. title = "Низкая цена",
  152. PriceFrom = 500,
  153. PriceTo = 3000
  154. },
  155. new ClientPrice()
  156. {
  157. title = "Средняя цена",
  158. PriceFrom = 3000,
  159. PriceTo = 15000
  160. },
  161. new ClientPrice()
  162. {
  163. title = "Высокая цена",
  164. PriceFrom = 15000,
  165. PriceTo = 30000
  166. }
  167. };
  168. }
  169. public IEnumerable<Client> getClient()
  170. {
  171. return new Client[]{
  172. new Client{
  173. Name = "Никита",
  174. Age = 20,
  175. Price=2300,
  176. Category="По России",
  177. Place="Москва"
  178. },
  179. new Client{
  180. Name = "Маша",
  181. Age = 15,
  182. Price=5000,
  183. Category="По России",
  184. Place="Сочи"
  185. },
  186. new Client{
  187. Name = "Алиса",
  188. Age = 35,
  189. Price=15000,
  190. Category="За границей",
  191. Place="Токио"
  192. },
  193. new Client{
  194. Name = "Леша",
  195. Age = 19,
  196. Price=20000,
  197. Category="За границей",
  198. Place="Южная Корея"
  199. },
  200. new Client{
  201. Name = "Андрей",
  202. Age = 28,
  203. Price=3000,
  204. Category="По России",
  205. Place="Крым"
  206. },
  207. new Client{
  208. Name = "Костя",
  209. Age = 14,
  210. Price=18000,
  211. Category="За границей",
  212. Place="Токио"
  213. },
  214. new Client{
  215. Name = "Даша",
  216. Age = 12,
  217. Price=25000,
  218. Category="За границей",
  219. Place="Лондон"
  220. },
  221. new Client{
  222. Name = "Ольга",
  223. Age = 55,
  224. Price=22000,
  225. Category="За границей",
  226. Place="Люксембург"
  227. },
  228. new Client{
  229. Name = "Катя",
  230. Age = 30,
  231. Price=1500,
  232. Category="По России",
  233. Place="Адлер"
  234. },
  235. new Client{
  236. Name = "Анжела",
  237. Age = 61,
  238. Price=25000,
  239. Category="За границей",
  240. Place="Мальдивы"
  241. },
  242. new Client{
  243. Name = "Миша",
  244. Age = 17,
  245. Price=30000,
  246. Category="За границей",
  247. Place="Дубаи"
  248. },
  249. };
  250. }
  251. }
  252. }