MainWindow.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 System.Threading.Tasks;
  13. using System;
  14. using WpfAppA.model;
  15. using System.IO;
  16. using System.Runtime.Serialization.Json;
  17. namespace WpfAppA
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window, INotifyPropertyChanged
  23. {
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. private void Invalidate()
  26. {
  27. if (PropertyChanged != null)
  28. PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
  29. }
  30. public string selectedPower = "Мощь";
  31. public HeroAge? selectedAge = null;
  32. public string selectedRace = "Раса";
  33. private IEnumerable<Hero> _HeroList;
  34. public IEnumerable<Hero> HeroList
  35. {
  36. get
  37. {
  38. var res = _HeroList;
  39. res = res
  40. .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
  41. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  42. .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
  43. if (searchFilter != "")
  44. res = res.Where(c => c.name.IndexOf(
  45. searchFilter,
  46. StringComparison.OrdinalIgnoreCase) >= 0);
  47. if (sortAsc) res = res.OrderBy(c => c.Age);
  48. else res = res.OrderByDescending(c => c.Age);
  49. return res;
  50. }
  51. set
  52. {
  53. _HeroList = value;
  54. }
  55. }
  56. public List<HeroPower> HeroPowerList { get; set; }
  57. public List<HeroAge> HeroAgeList { get; set; }
  58. public List<HeroRace> HeroRaceList { get; set; }
  59. public MainWindow()
  60. {
  61. InitializeComponent();
  62. DataContext = this;
  63. Globals.dataProvider = new JSONDataProvider();
  64. HeroList = Globals.dataProvider.getHero();
  65. HeroPowerList = Globals.dataProvider.getPower().ToList();
  66. HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
  67. HeroAgeList = Globals.dataProvider.getAge().ToList();
  68. HeroRaceList = Globals.dataProvider.getRace().ToList();
  69. HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
  70. }
  71. private void ExitButton_Click(object sender, RoutedEventArgs e)
  72. {
  73. Application.Current.Shutdown();
  74. }
  75. private void Button_Click(object sender, RoutedEventArgs e)
  76. {
  77. Button clickedButton = (Button)sender;
  78. MessageBox.Show(clickedButton.Content.ToString());
  79. }
  80. private string currentStyle = "StackStyle";
  81. private void ToggleView_Click(object sender, RoutedEventArgs e)
  82. {
  83. currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
  84. var newStyle = (Style)TryFindResource(currentStyle);
  85. if (newStyle != null)
  86. HeroListBox.Style = newStyle;
  87. }
  88. private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  89. {
  90. selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
  91. Invalidate();
  92. }
  93. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  94. {
  95. selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
  96. Invalidate();
  97. }
  98. private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  99. {
  100. selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
  101. Invalidate();
  102. }
  103. private string searchFilter = "";
  104. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  105. {
  106. searchFilter = SearchFilterTextBox.Text;
  107. Invalidate();
  108. }
  109. private bool sortAsc = true;
  110. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  111. {
  112. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  113. Invalidate();
  114. }
  115. public class JSONDataProvider : LocalDataProvider, IDataProvider
  116. {
  117. private List<Hero> _HeroList;
  118. public JSONDataProvider()
  119. {
  120. var serializer = new DataContractJsonSerializer(typeof(Hero[]));
  121. using (var sr = new StreamReader("./data.json"))
  122. {
  123. _HeroList = ((Hero[])serializer.ReadObject(sr.BaseStream)).ToList();
  124. }
  125. }
  126. public IEnumerable<Hero> getHero()
  127. {
  128. return _HeroList;
  129. }
  130. }
  131. }
  132. interface IDataProvider
  133. {
  134. IEnumerable<Hero> getHero();
  135. IEnumerable<HeroPower> getPower();
  136. IEnumerable<HeroAge> getAge();
  137. IEnumerable<HeroRace> getRace();
  138. }
  139. public class LocalDataProvider : IDataProvider
  140. {
  141. public IEnumerable<HeroPower> getPower()
  142. {
  143. return new HeroPower[]
  144. {
  145. new HeroPower()
  146. {
  147. title="1/10"
  148. },
  149. new HeroPower()
  150. {
  151. title="2/10"
  152. },
  153. new HeroPower()
  154. {
  155. title="3/10"
  156. },
  157. new HeroPower()
  158. {
  159. title="4/10"
  160. },
  161. new HeroPower()
  162. {
  163. title="5/10"
  164. },
  165. new HeroPower()
  166. {
  167. title="6/10"
  168. },
  169. new HeroPower()
  170. {
  171. title="7/10"
  172. },
  173. new HeroPower()
  174. {
  175. title="8/10"
  176. },
  177. new HeroPower()
  178. {
  179. title="9/10"
  180. },
  181. new HeroPower()
  182. {
  183. title="10/10"
  184. },
  185. };
  186. }
  187. public IEnumerable<HeroAge> getAge()
  188. {
  189. return new HeroAge[]
  190. {
  191. new HeroAge()
  192. {
  193. title="Все возраста",
  194. AgeFrom=0,
  195. AgeTo=2000
  196. },
  197. new HeroAge()
  198. {
  199. title="Дети",
  200. AgeFrom=1,
  201. AgeTo=100
  202. },
  203. new HeroAge()
  204. {
  205. title="Подростки",
  206. AgeFrom=100,
  207. AgeTo=500
  208. },
  209. new HeroAge()
  210. {
  211. title="Молодые",
  212. AgeFrom=500,
  213. AgeTo=700
  214. },
  215. new HeroAge()
  216. {
  217. title="Старики",
  218. AgeFrom=700,
  219. AgeTo=2000
  220. }
  221. };
  222. }
  223. public IEnumerable<HeroRace> getRace()
  224. {
  225. return new HeroRace[]
  226. {
  227. new HeroRace()
  228. {
  229. title="Человек"
  230. },
  231. new HeroRace()
  232. {
  233. title="Дракон"
  234. },
  235. new HeroRace()
  236. {
  237. title="Фея"
  238. },
  239. new HeroRace()
  240. {
  241. title="Огр"
  242. },
  243. new HeroRace()
  244. {
  245. title="Рыба"
  246. },
  247. new HeroRace()
  248. {
  249. title="Селёдка"
  250. },
  251. new HeroRace()
  252. {
  253. title="Камень"
  254. },
  255. new HeroRace()
  256. {
  257. title="Гоблин"
  258. },
  259. };
  260. }
  261. public IEnumerable<Hero> getHero()
  262. {
  263. return new Hero[]{
  264. new Hero{
  265. race="Человек",
  266. name="Invoker",
  267. Age= 1000,
  268. power="10/10"},
  269. new Hero{
  270. race="Человек",
  271. name="Juggernaut",
  272. Age= 450,
  273. power="9/10"},
  274. new Hero{
  275. race="Человек",
  276. name="lina",
  277. Age = 300,
  278. power="8/10"},
  279. new Hero{
  280. race="Огр",
  281. name="Troll Warloard",
  282. Age = 100,
  283. power="7/10"},
  284. new Hero{
  285. race="Рыба",
  286. name="Slark",
  287. Age = 120,
  288. power="6/10"},
  289. new Hero{
  290. race="Селёдка",
  291. name="Slardar",
  292. Age = 200,
  293. power="5/10"},
  294. new Hero{
  295. race="Камень",
  296. name="Tiny",
  297. Age = 20000,
  298. power="4/10"},
  299. new Hero{
  300. race="Фея",
  301. name="Dark Willow",
  302. Age = 700,
  303. power="3/10"},
  304. new Hero{
  305. race="Огр",
  306. name="Orge Magi",
  307. Age = 1400,
  308. power="2/10"},
  309. new Hero{
  310. race="Дракон",
  311. name="Viper",
  312. Age = 1900,
  313. power="1/10"},
  314. };
  315. }
  316. }
  317. }