MainWindow.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 wpf_app.Model.Windows;
  13. using wpf_app.Model;
  14. namespace wpf_app
  15. {
  16. /// <summary>
  17. /// Interaction logic for MainWindow.xaml
  18. /// </summary>
  19. ///
  20. public partial class MainWindow : Window, INotifyPropertyChanged
  21. {
  22. private IEnumerable<Product> _productList;
  23. public IEnumerable<Product> ProductList
  24. {
  25. get
  26. {
  27. var res = _productList;
  28. res = _productList
  29. .Where(p =>(selectedCategory == "Все категории" || p.Category == selectedCategory))
  30. .Where(p =>(selectedPrice == null || (p.Price>=selectedPrice.priceFrom && p.Price<selectedPrice.priceTo)))
  31. .Where(p =>(selectedCompany == "Все компании" || p.Company == selectedCompany));
  32. if (searchFilter != "")
  33. res = res.Where(c => c.Title.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0);
  34. if (sortAsc) res = res.OrderBy(p => p.Price);
  35. else res = res.OrderByDescending(p => p.Price);
  36. return res;
  37. }
  38. set
  39. {
  40. _productList = value;
  41. }
  42. }
  43. public List <ProductCategory> CategoryList { get; set; }
  44. public List <ProductPrice> ProductPrices { get; set; }
  45. public List <ProductCompany> ProductCompanies { get; set; }
  46. string selectedCategory = "";
  47. string selectedCompany = "";
  48. private ProductPrice? selectedPrice = null;
  49. private string searchFilter = "";
  50. private bool sortAsc = true;
  51. public MainWindow()
  52. {
  53. InitializeComponent();
  54. DataContext = this;
  55. Globals.dataProvider = new JSONDataProvider();
  56. ProductList = Globals.dataProvider.GetProducts();
  57. ProductPrices = Globals.dataProvider.GetPrices().ToList();
  58. CategoryList = Globals.dataProvider.GetCategories().ToList();
  59. CategoryList.Insert(0, new ProductCategory { title = "Все категории" });
  60. ProductCompanies = Globals.dataProvider.GetCompanies().ToList();
  61. ProductCompanies.Insert(0, new ProductCompany { title = "Все компании" });
  62. }
  63. private void ExitButton_Click(object sender, RoutedEventArgs e)
  64. {
  65. Application.Current.Shutdown();
  66. }
  67. private void CategoryFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  68. {
  69. selectedCategory = (CategoryFilterComboBox.SelectedItem as ProductCategory).title;
  70. Invalidate();
  71. }
  72. private void PriceFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  73. {
  74. selectedPrice = PriceFilterComboBox.SelectedItem as ProductPrice;
  75. Invalidate();
  76. }
  77. private void CompanyFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  78. {
  79. selectedCompany = (CompanyFilterComboBox.SelectedItem as ProductCompany).title;
  80. Invalidate();
  81. }
  82. private void SearchFilterTextBox_KeyUp(object sender, KeyEventArgs e)
  83. {
  84. searchFilter = SearchFilterTextBox.Text;
  85. Invalidate();
  86. }
  87. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  88. {
  89. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  90. Invalidate();
  91. }
  92. private void productsListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  93. {
  94. var productDetailsWindow = new ProductDetailsWindow(productsListBox.SelectedItem as Product);
  95. productDetailsWindow.Show();
  96. }
  97. public event PropertyChangedEventHandler PropertyChanged;
  98. private void Invalidate()
  99. {
  100. if (PropertyChanged != null)
  101. PropertyChanged(this, new PropertyChangedEventArgs("ProductList"));
  102. }
  103. }
  104. class Globals
  105. {
  106. public static IDataProvider dataProvider;
  107. }
  108. }