MainWindow.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System.ComponentModel;
  2. using System.IO;
  3. using System.Runtime.Serialization.Json;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using WpfApp2.model;
  15. using WpfApp2.Windows;
  16. namespace WpfApp2
  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 ClientListBox_MouseDoubleClick(
  103. object sender,
  104. MouseButtonEventArgs e)
  105. {
  106. // в создаваемое окно передаем выбранного котика
  107. var detailWindow = new DetailWindow(
  108. ClientListBox.SelectedItem as Client);
  109. detailWindow.ShowDialog();
  110. }
  111. private void ClientPriceFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  112. {
  113. selectedPrice = PriceFilterComboBox.SelectedItem as ClientPrice;
  114. Invalidate();
  115. }
  116. private bool sortAsc = true;
  117. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  118. {
  119. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  120. Invalidate();
  121. }
  122. private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  123. {
  124. }
  125. }
  126. interface IDataProvider
  127. {
  128. IEnumerable<Client> getClient();
  129. IEnumerable<ClientPrice> getPrice();
  130. IEnumerable<ClientCategory> getCategory();
  131. }
  132. public class LocalDataProvider : IDataProvider
  133. {
  134. public IEnumerable<ClientCategory> getCategory()
  135. {
  136. return new ClientCategory[]
  137. {
  138. new ClientCategory()
  139. {
  140. title="По России"
  141. },
  142. new ClientCategory()
  143. {
  144. title="За границей"
  145. },
  146. };
  147. }
  148. public IEnumerable<ClientPrice> getPrice()
  149. {
  150. return new ClientPrice[]
  151. {
  152. new ClientPrice()
  153. {
  154. title = "Все цены",
  155. PriceFrom = 500,
  156. PriceTo = 30000
  157. },
  158. new ClientPrice()
  159. {
  160. title = "Низкая цена",
  161. PriceFrom = 500,
  162. PriceTo = 3000
  163. },
  164. new ClientPrice()
  165. {
  166. title = "Средняя цена",
  167. PriceFrom = 3000,
  168. PriceTo = 15000
  169. },
  170. new ClientPrice()
  171. {
  172. title = "Высокая цена",
  173. PriceFrom = 15000,
  174. PriceTo = 30000
  175. }
  176. };
  177. }
  178. public IEnumerable<Client> getClient()
  179. {
  180. return new Client[]{
  181. new Client{
  182. Name = "Никита",
  183. Age = 20,
  184. Price=2300,
  185. Category="По России",
  186. Place="Москва"
  187. },
  188. new Client{
  189. Name = "Маша",
  190. Age = 15,
  191. Price=5000,
  192. Category="По России",
  193. Place="Сочи"
  194. },
  195. new Client{
  196. Name = "Алиса",
  197. Age = 35,
  198. Price=15000,
  199. Category="За границей",
  200. Place="Токио"
  201. },
  202. new Client{
  203. Name = "Леша",
  204. Age = 19,
  205. Price=20000,
  206. Category="За границей",
  207. Place="Южная Корея"
  208. },
  209. new Client{
  210. Name = "Андрей",
  211. Age = 28,
  212. Price=3000,
  213. Category="По России",
  214. Place="Крым"
  215. },
  216. new Client{
  217. Name = "Костя",
  218. Age = 14,
  219. Price=18000,
  220. Category="За границей",
  221. Place="Токио"
  222. },
  223. new Client{
  224. Name = "Даша",
  225. Age = 12,
  226. Price=25000,
  227. Category="За границей",
  228. Place="Лондон"
  229. },
  230. new Client{
  231. Name = "Ольга",
  232. Age = 55,
  233. Price=22000,
  234. Category="За границей",
  235. Place="Люксембург"
  236. },
  237. new Client{
  238. Name = "Катя",
  239. Age = 30,
  240. Price=1500,
  241. Category="По России",
  242. Place="Адлер"
  243. },
  244. new Client{
  245. Name = "Анжела",
  246. Age = 61,
  247. Price=25000,
  248. Category="За границей",
  249. Place="Мальдивы"
  250. },
  251. new Client{
  252. Name = "Миша",
  253. Age = 17,
  254. Price=30000,
  255. Category="За границей",
  256. Place="Дубаи"
  257. },
  258. };
  259. }
  260. }
  261. }