MainWindow.xaml.cs 4.0 KB

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