123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using System.ComponentModel;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Threading.Tasks;
- using System;
- using WpfAppA.model;
- using System.IO;
- using System.Runtime.Serialization.Json;
- namespace WpfAppA
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private void Invalidate()
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
- }
- public string selectedPower = "Мощь";
- public HeroAge? selectedAge = null;
- public string selectedRace = "Раса";
- private IEnumerable<Hero> _HeroList;
- public IEnumerable<Hero> HeroList
- {
- get
- {
- var res = _HeroList;
- res = res
- .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
- .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
- .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
- if (searchFilter != "")
- res = res.Where(c => c.name.IndexOf(
- searchFilter,
- StringComparison.OrdinalIgnoreCase) >= 0);
- if (sortAsc) res = res.OrderBy(c => c.Age);
- else res = res.OrderByDescending(c => c.Age);
- return res;
- }
- set
- {
- _HeroList = value;
- }
- }
- public List<HeroPower> HeroPowerList { get; set; }
- public List<HeroAge> HeroAgeList { get; set; }
- public List<HeroRace> HeroRaceList { get; set; }
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- Globals.dataProvider = new JSONDataProvider();
- HeroList = Globals.dataProvider.getHero();
- HeroPowerList = Globals.dataProvider.getPower().ToList();
- HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
- HeroAgeList = Globals.dataProvider.getAge().ToList();
- HeroRaceList = Globals.dataProvider.getRace().ToList();
- HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
- }
- private void ExitButton_Click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
- Invalidate();
- }
- private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
- Invalidate();
- }
- private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
- Invalidate();
- }
- private string searchFilter = "";
- private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
- {
- searchFilter = SearchFilterTextBox.Text;
- Invalidate();
- }
- private bool sortAsc = true;
- private void RadioButton_Checked(object sender, RoutedEventArgs e)
- {
- sortAsc = (sender as RadioButton).Tag.ToString() == "1";
- Invalidate();
- }
- public class JSONDataProvider : LocalDataProvider, IDataProvider
- {
- private List<Hero> _HeroList;
- public JSONDataProvider()
- {
- var serializer = new DataContractJsonSerializer(typeof(Hero[]));
- using (var sr = new StreamReader("./data.json"))
- {
- _HeroList = ((Hero[])serializer.ReadObject(sr.BaseStream)).ToList();
- }
- }
- public IEnumerable<Hero> getHero()
- {
- return _HeroList;
- }
- }
- }
- interface IDataProvider
- {
- IEnumerable<Hero> getHero();
- IEnumerable<HeroPower> getPower();
- IEnumerable<HeroAge> getAge();
- IEnumerable<HeroRace> getRace();
- }
- public class LocalDataProvider : IDataProvider
- {
- public IEnumerable<HeroPower> getPower()
- {
- return new HeroPower[]
- {
- new HeroPower()
- {
- title="1/10"
- },
- new HeroPower()
- {
- title="2/10"
- },
- new HeroPower()
- {
- title="3/10"
- },
- new HeroPower()
- {
- title="4/10"
- },
- new HeroPower()
- {
- title="5/10"
- },
- new HeroPower()
- {
- title="6/10"
- },
- new HeroPower()
- {
- title="7/10"
- },
- new HeroPower()
- {
- title="8/10"
- },
- new HeroPower()
- {
- title="9/10"
- },
- new HeroPower()
- {
- title="10/10"
- },
- };
- }
- public IEnumerable<HeroAge> getAge()
- {
- return new HeroAge[]
- {
- new HeroAge()
- {
- title="Все возраста",
- AgeFrom=0,
- AgeTo=2000
- },
- new HeroAge()
- {
- title="Дети",
- AgeFrom=1,
- AgeTo=100
- },
- new HeroAge()
- {
- title="Подростки",
- AgeFrom=100,
- AgeTo=500
- },
- new HeroAge()
- {
- title="Молодые",
- AgeFrom=500,
- AgeTo=700
- },
- new HeroAge()
- {
- title="Старики",
- AgeFrom=700,
- AgeTo=2000
- }
- };
- }
- public IEnumerable<HeroRace> getRace()
- {
- return new HeroRace[]
- {
- new HeroRace()
- {
- title="Человек"
- },
- new HeroRace()
- {
- title="Дракон"
- },
- new HeroRace()
- {
- title="Фея"
- },
- new HeroRace()
- {
- title="Огр"
- },
- new HeroRace()
- {
- title="Рыба"
- },
- new HeroRace()
- {
- title="Селёдка"
- },
- new HeroRace()
- {
- title="Камень"
- },
- new HeroRace()
- {
- title="Гоблин"
- },
- };
- }
- public IEnumerable<Hero> getHero()
- {
- return new Hero[]{
- new Hero{
- race="Человек",
- name="Invoker",
- Age=1000,
- power="10/10"},
- new Hero{
- race="Человек",
- name="Juggernaut",
- Age=450,
- power="9/10"},
- new Hero{
- race="Человек",
- name="lina",
- Age = 300,
- power="8/10"},
- new Hero{
- race="Огр",
- name="Troll Warloard",
- Age = 100,
- power="7/10"},
- new Hero{
- race="Рыба",
- name="Slark",
- Age = 120,
- power="6/10"},
- new Hero{
- race="Селёдка",
- name="Slardar",
- Age = 200,
- power="5/10"},
- new Hero{
- race="Камень",
- name="Tiny",
- Age = 20000,
- power="4/10"},
- new Hero{
- race="Фея",
- name="Dark Willow",
- Age = 700,
- power="3/10"},
- new Hero{
- race="Огр",
- name="Orge Magi",
- Age = 1400,
- power="2/10"},
- new Hero{
- race="Дракон",
- name="Viper",
- Age = 1900,
- power="1/10"},
- };
- }
- }
- }
|