MainWindow.xaml.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_template.Model;
  13. namespace wpf_template
  14. {
  15. /// <summary>
  16. /// Interaction logic for MainWindow.xaml
  17. /// </summary>
  18. ///
  19. public partial class MainWindow : Window, INotifyPropertyChanged
  20. {
  21. private IEnumerable<Product> _productList;
  22. public IEnumerable<Product> ProductList
  23. {
  24. get
  25. {
  26. return _productList
  27. .Where(p =>(selectedCategory == "Все категории" || p.Category == selectedCategory))
  28. .Where(p =>(selectedPrice == null || (p.Price>=selectedPrice.priceFrom && p.Price<selectedPrice.priceTo)))
  29. .Where(p =>(selectedCompany == "Все компании" || p.Company == selectedCompany));
  30. }
  31. set
  32. {
  33. _productList = value;
  34. }
  35. }
  36. public List <ProductCategory> CategoryList { get; set; }
  37. public List <ProductPrice> ProductPrices { get; set; }
  38. public List <ProductCompany> ProductCompanies { get; set; }
  39. string selectedCategory = "";
  40. string selectedCompany = "";
  41. private ProductPrice? selectedPrice = null;
  42. public MainWindow()
  43. {
  44. InitializeComponent();
  45. DataContext = this;
  46. Globals.dataProvider = new LocalDataProvider();
  47. ProductList = Globals.dataProvider.GetProducts();
  48. ProductPrices = Globals.dataProvider.GetPrices().ToList();
  49. CategoryList = Globals.dataProvider.GetCategories().ToList();
  50. CategoryList.Insert(0, new ProductCategory { title = "Все категории" });
  51. ProductCompanies = Globals.dataProvider.GetCompanies().ToList();
  52. ProductCompanies.Insert(0, new ProductCompany { title = "Все компании" });
  53. }
  54. private void ExitButton_Click(object sender, RoutedEventArgs e)
  55. {
  56. Application.Current.Shutdown();
  57. }
  58. private void CategoryFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  59. {
  60. selectedCategory = (CategoryFilterComboBox.SelectedItem as ProductCategory).title;
  61. Invalidate();
  62. }
  63. private void PriceFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  64. {
  65. selectedPrice = PriceFilterComboBox.SelectedItem as ProductPrice;
  66. Invalidate();
  67. }
  68. private void CompanyFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  69. {
  70. selectedCompany = (CompanyFilterComboBox.SelectedItem as ProductCompany).title;
  71. Invalidate();
  72. }
  73. public event PropertyChangedEventHandler PropertyChanged;
  74. private void Invalidate()
  75. {
  76. if (PropertyChanged != null)
  77. PropertyChanged(this, new PropertyChangedEventArgs("ProductList"));
  78. }
  79. }
  80. class Globals
  81. {
  82. public static IDataProvider dataProvider;
  83. }
  84. }