MainWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. namespace WpfApp3
  11. {
  12. public partial class MainWindow : Window
  13. {
  14. public ObservableCollection<Student> StudentList { get; set; }
  15. private Dictionary<string, object> filters = new Dictionary<string, object>();
  16. private string currentStyle = "StackStyle";
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. DataContext = this;
  21. Globals.dataProvider = new LocalDataProvider();
  22. StudentList = new ObservableCollection<Student>(Globals.dataProvider.GetStudents());
  23. List<string> styles = new List<string> { "light", "dark" };
  24. styleBox.SelectionChanged += ThemeChange;
  25. styleBox.ItemsSource = styles;
  26. styleBox.SelectedItem = "dark";
  27. Style buttonStyle = new Style();
  28. buttonStyle.Setters.Add(
  29. new Setter
  30. {
  31. Property = Control.FontFamilyProperty,
  32. Value = new FontFamily("Verdana")
  33. });
  34. buttonStyle.Setters.Add(
  35. new Setter
  36. {
  37. Property = Control.MarginProperty,
  38. Value = new Thickness(10)
  39. });
  40. buttonStyle.Setters.Add(
  41. new Setter
  42. {
  43. Property = Control.BackgroundProperty,
  44. Value = new SolidColorBrush(Colors.PowderBlue)
  45. });
  46. buttonStyle.Setters.Add(
  47. new Setter
  48. {
  49. Property = Control.ForegroundProperty,
  50. Value = new SolidColorBrush(Colors.Black)
  51. });
  52. }
  53. private void ExitButton_Click(object sender, RoutedEventArgs e)
  54. {
  55. Application.Current.Shutdown();
  56. }
  57. private void NameFilter_TextChanged(object sender, TextChangedEventArgs e)
  58. {
  59. filters["Name"] = ((TextBox)sender).Text;
  60. ApplyFilter();
  61. }
  62. private void NationFilter_TextChanged(object sender, TextChangedEventArgs e)
  63. {
  64. filters["Nation"] = ((TextBox)sender).Text;
  65. ApplyFilter();
  66. }
  67. private void ApplyFilter()
  68. {
  69. var filteredStudents = Globals.dataProvider.GetStudents();
  70. foreach (var filter in filters)
  71. {
  72. filteredStudents = filteredStudents.Where(s => s.GetType().GetProperty(filter.Key).GetValue(s, null).ToString().Contains(filter.Value.ToString()));
  73. }
  74. filteredStudents = filteredStudents.Where(s => s.Age >= 20 && s.Course > 2);
  75. StudentList = new ObservableCollection<Student>(filteredStudents);
  76. }
  77. private void ThemeChange(object sender, SelectionChangedEventArgs e)
  78. {
  79. string style = styleBox.SelectedItem as string;
  80. var uri = new Uri(style + ".xaml", UriKind.Relative);
  81. ResourceDictionary resourceDict =
  82. Application.LoadComponent(uri) as ResourceDictionary;
  83. Application.Current.Resources.Clear();
  84. Application.Current.Resources.MergedDictionaries.Add(resourceDict);
  85. }
  86. private void ToggleViewButton_Click(object sender, RoutedEventArgs e)
  87. {
  88. currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
  89. var newStyle = (Style)TryFindResource(currentStyle);
  90. if (newStyle != null)
  91. StudentListBox.Style = newStyle;
  92. }
  93. private void StudentListBox_MouseDoubleClick(
  94. object sender,
  95. MouseButtonEventArgs e)
  96. {
  97. // в создаваемое окно передаем выбранного котика
  98. var detailsWindow = new Windows.StudentDetailsWindow(
  99. StudentListBox.SelectedItem as Student);
  100. detailsWindow.ShowDialog();
  101. }
  102. }
  103. internal interface IDataProvider
  104. {
  105. IEnumerable<Student> GetStudents();
  106. }
  107. public class LocalDataProvider : IDataProvider
  108. {
  109. public IEnumerable<Student> GetStudents()
  110. {
  111. return new List<Student>
  112. {
  113. new Student { Id = 1, Name = "Хэй Вэнь", Age = 24, Country = "Китай", GPA = 5.0, Speciality = "Программирование", Grant = 25000.0, Course = 4, EnrollmentDate = new DateTime(2020, 9, 1), IsFullTime = true },
  114. new Student { Id = 2, Name = "Семен Хомяк", Age = 22, Country = "Россия", GPA = 3.7, Speciality = "Инженерия", Grant = 10000.0, Course = 2, EnrollmentDate = new DateTime(2022, 1, 15), IsFullTime = false },
  115. new Student { Id = 3, Name = "Джейкоб Ли", Age = 19, Country = "Америка", GPA = 4.4, Speciality = "Лингвистика", Grant = 20000.0, Course = 1, EnrollmentDate = new DateTime(2023, 8, 20), IsFullTime = true },
  116. new Student { Id = 4, Name = "Арамсунторнсук Пирапонг", Age = 18, Country = "Таиланд", GPA = 4.1, Speciality = "Логистика", Grant = 15000.0, Course = 1, EnrollmentDate = new DateTime(2023, 4, 6), IsFullTime = false },
  117. new Student { Id = 5, Name = "Рипка Дарина", Age = 18, Country = "Украина", GPA = 4.8, Speciality = "Дизайн", Grant = 23000.0, Course = 1, EnrollmentDate = new DateTime(2023, 5, 15), IsFullTime = true },
  118. };
  119. }
  120. }
  121. public class Student
  122. {
  123. public int Id { get; set; }
  124. public string Name { get; set; }
  125. public string Speciality { get; set; }
  126. public string Country { get; set; }
  127. public double GPA { get; set; }
  128. public double Grant { get; set; }
  129. public DateTime EnrollmentDate { get; set; }
  130. public bool IsFullTime { get; set; }
  131. public int Age { get; internal set; }
  132. public int Course { get; internal set; }
  133. }
  134. }