MainWindow.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.ComponentModel;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using WpfApp2.model;
  6. namespace WpfApp2
  7. {
  8. /// <summary>
  9. /// Interaction logic for MainWindow.xaml
  10. /// </summary>
  11. public partial class MainWindow : Window, INotifyPropertyChanged
  12. {
  13. public event PropertyChangedEventHandler PropertyChanged;
  14. private void Invalidate()
  15. {
  16. if (PropertyChanged != null)
  17. PropertyChanged(this, new PropertyChangedEventArgs("SparesList"));
  18. }
  19. public string selectedDetail = "Деталь";
  20. public SparesPrice? selectedPrice = null;
  21. public string selectedType = "Тип ТС";
  22. private IEnumerable<Spares> _SparesList;
  23. public IEnumerable<Spares> SparesList
  24. {
  25. get
  26. {
  27. var res = _SparesList;
  28. res = res
  29. .Where(c => (c.detail == selectedDetail || selectedDetail == "Деталь"))
  30. .Where(c => (selectedPrice == null || (c.price >= selectedPrice.PriceFrom && c.price < selectedPrice.PriceTo)))
  31. .Where(c => (c.type == selectedType || selectedType == "Тип ТС"));
  32. if (searchFilter != "")
  33. res = res.Where(c => c.name.IndexOf(
  34. searchFilter,
  35. StringComparison.OrdinalIgnoreCase) >= 0);
  36. if (sortAsc) res = res.OrderBy(c => c.price);
  37. else res = res.OrderByDescending(c => c.price);
  38. return res;
  39. }
  40. set
  41. {
  42. _SparesList = value;
  43. }
  44. }
  45. public List<SparesDetail> SparesDetailList { get; set; }
  46. public List<SparesPrice> SparesPriceList { get; set; }
  47. public List<SparesType> SparesTypeList { get; set; }
  48. public MainWindow()
  49. {
  50. InitializeComponent();
  51. DataContext = this;
  52. Globals.dataProvider = new LocalDataProvider();
  53. SparesList = Globals.dataProvider.getSpares();
  54. SparesDetailList = Globals.dataProvider.getDetail().ToList();
  55. SparesDetailList.Insert(0, new SparesDetail { title = "Деталь" });
  56. SparesPriceList = Globals.dataProvider.getPrice().ToList();
  57. SparesTypeList = Globals.dataProvider.getType().ToList();
  58. SparesTypeList.Insert(0, new SparesType { title = "Тип ТС" });
  59. }
  60. private bool sortAsc = true;
  61. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  62. {
  63. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  64. Invalidate();
  65. }
  66. private void ExitButton_Click(object sender, RoutedEventArgs e)
  67. {
  68. Application.Current.Shutdown();
  69. }
  70. private void DetailFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  71. {
  72. selectedDetail = (DetailFilterComboBox.SelectedItem as SparesDetail).title;
  73. Invalidate();
  74. }
  75. private void PriceFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  76. {
  77. selectedPrice = PriceFilterComboBox.SelectedItem as SparesPrice;
  78. Invalidate();
  79. }
  80. private void TypeFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  81. {
  82. selectedType = (TypeFilterComboBox.SelectedItem as SparesType).title;
  83. Invalidate();
  84. }
  85. private string searchFilter = "";
  86. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  87. {
  88. searchFilter = SearchFilterTextBox.Text;
  89. Invalidate();
  90. }
  91. }
  92. interface IDataProvider
  93. {
  94. IEnumerable<Spares> getSpares();
  95. IEnumerable<SparesDetail> getDetail();
  96. IEnumerable<SparesPrice> getPrice();
  97. IEnumerable<SparesType> getType();
  98. }
  99. public class LocalDataProvider : IDataProvider
  100. {
  101. public IEnumerable<SparesDetail> getDetail()
  102. {
  103. return new SparesDetail[]
  104. {
  105. new SparesDetail()
  106. {
  107. title="Двигатель"
  108. },
  109. new SparesDetail()
  110. {
  111. title="КПП"
  112. },
  113. new SparesDetail()
  114. {
  115. title="Подвеска"
  116. },
  117. new SparesDetail()
  118. {
  119. title="Стекла"
  120. },
  121. new SparesDetail()
  122. {
  123. title="Колеса"
  124. },
  125. };
  126. }
  127. public IEnumerable<SparesPrice> getPrice()
  128. {
  129. return new SparesPrice[]
  130. {
  131. new SparesPrice()
  132. {
  133. title="Все цены",
  134. PriceFrom=0,
  135. PriceTo=99999999
  136. },
  137. new SparesPrice()
  138. {
  139. title="БУ",
  140. PriceFrom=0,
  141. PriceTo=10000
  142. },
  143. new SparesPrice()
  144. {
  145. title="Дешевая",
  146. PriceFrom=10000,
  147. PriceTo=20000
  148. },
  149. new SparesPrice()
  150. {
  151. title="Средняя",
  152. PriceFrom=20000,
  153. PriceTo=50000
  154. },
  155. new SparesPrice()
  156. {
  157. title="Элитная",
  158. PriceFrom=50000,
  159. PriceTo=99999999
  160. }
  161. };
  162. }
  163. public IEnumerable<SparesType> getType()
  164. {
  165. return new SparesType[]
  166. {
  167. new SparesType()
  168. {
  169. title="Легкая"
  170. },
  171. new SparesType()
  172. {
  173. title="Грузовая"
  174. },
  175. };
  176. }
  177. public IEnumerable<Spares> getSpares()
  178. {
  179. return new Spares[]{
  180. new Spares{
  181. type="Легкая",
  182. name="Двигатель МБ140(ОК2281.4)",
  183. price = 72000,
  184. detail="Двигатель"},
  185. new Spares{
  186. type="Легкая",
  187. name="КПП 5-СТ РС-ПДР",
  188. price = 20000,
  189. detail="КПП"},
  190. new Spares{
  191. type="Грузовая",
  192. name="Подвеска ПДРС1337",
  193. price = 15000,
  194. detail="Подвеска"},
  195. new Spares{
  196. type="Легкая",
  197. name="Шина 15-Р",
  198. price = 3000,
  199. detail="Колеса"},
  200. new Spares{
  201. type="Грузовая",
  202. name="Лобовое стекло СТ150",
  203. price = 40000,
  204. detail="Стекла"},
  205. };
  206. }
  207. }
  208. }