MainWindow.xaml.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace WpfApp2
  7. {
  8. public partial class MainWindow : Window
  9. {
  10. private List<MusicalInstrument> instruments;
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. InitializeData();
  15. FilterComboBox.Items.Add("Все");
  16. FilterComboBox.Items.Add("Клавишные");
  17. FilterComboBox.Items.Add("Струнные");
  18. FilterComboBox.Items.Add("Ударные");
  19. FilterComboBox.SelectedIndex = 0;
  20. UpdateInstrumentList(instruments);
  21. }
  22. private void InitializeData()
  23. {
  24. instruments = new List<MusicalInstrument>
  25. {
  26. new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 },
  27. new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 },
  28. new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 },
  29. new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 },
  30. new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },
  31. };
  32. }
  33. private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  34. {
  35. string selectedFilter = (string)FilterComboBox.SelectedItem;
  36. List<MusicalInstrument> filteredInstruments = new List<MusicalInstrument>();
  37. switch (selectedFilter)
  38. {
  39. case "Все":
  40. filteredInstruments = instruments;
  41. break;
  42. case "Клавишные":
  43. filteredInstruments = instruments.Where(i => i.Type == "Клавишные").ToList();
  44. break;
  45. case "Струнные":
  46. filteredInstruments = instruments.Where(i => i.Type == "Струнные").ToList();
  47. break;
  48. case "Ударные":
  49. filteredInstruments = instruments.Where(i => i.Type == "Ударные").ToList();
  50. break;
  51. }
  52. UpdateInstrumentList(filteredInstruments);
  53. }
  54. private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
  55. {
  56. InstrumentListView.ItemsSource = instrumentsToDisplay;
  57. }
  58. }
  59. class MusicalInstrument
  60. {
  61. public string Name { get; set; }
  62. public string Type { get; set; }
  63. public string Brand { get; set; }
  64. public double Price { get; set; }
  65. }
  66. }