123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using System;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using wpf_listbox.models;
- using System.Threading.Tasks;
- using System;
- using System.Formats.Asn1;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization.Json;
- namespace wpf_listbox
- {
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private void Invalidate()
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs("PeopleList"));
- }
- public string selectedGender = "Пол";
- public PeopleAge? selectedAge = null;
- public string selectedPlace = "Место";
- private IEnumerable<People> _PeopleList;
- public IEnumerable<People> PeopleList
- {
- get
- {
- var res = _PeopleList;
- // фильтруем по возрасту
- res = res
- .Where(c => (c.Gender == selectedGender || selectedGender == "Пол"))
- .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
- .Where(c => (c.Place == selectedPlace || selectedPlace == "Место"));
- // если фильтр не пустой, то ищем ВХОЖДЕНИЕ подстроки поиска в кличке без учета регистра
- if (searchFilter != "")
- res = res.Where(c => c.Name.IndexOf(
- searchFilter,
- StringComparison.OrdinalIgnoreCase) >= 0 || c.Gender.IndexOf(
- searchFilter,
- StringComparison.OrdinalIgnoreCase) >= 0 || c.Place.IndexOf(
- searchFilter,
- StringComparison.OrdinalIgnoreCase) >= 0);
- if (sortAsc) res = res.OrderBy(c => c.Price);
- else res = res.OrderByDescending(c => c.Price);
- if (sortAsc) res = res.OrderBy(c => c.Price);
- else res = res.OrderByDescending(c => c.Price);
- return res;
- }
- set
- {
- _PeopleList = value;
- }
- }
- public List<PeopleGender> PeopleGenderList { get; set; }
- public List<PeopleAge> PeopleAgeList { get; set; }
- public List<PeoplePlace> PeoplePlaceList { get; set; }
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- Globals.dataProvider = new JSONDataProvider();
- PeopleList = Globals.dataProvider.getPeople();
- PeopleGenderList = Globals.dataProvider.getGender().ToList();
- PeopleGenderList.Insert(0, new PeopleGender { title = "Пол" });
- PeopleAgeList = Globals.dataProvider.getAge().ToList();
- PeoplePlaceList = Globals.dataProvider.getPlace().ToList();
- PeoplePlaceList.Insert(0, new PeoplePlace { title = "Место" });
- }
- private void ExitButton_Click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- private string searchFilter = "";
- private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
- {
- searchFilter = SearchFilterTextBox.Text;
- Invalidate();
- }
- private void GenderFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedGender = (GenderFilterComboBox.SelectedItem as PeopleGender).title;
- Invalidate();
- }
- private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedAge = AgeFilterComboBox.SelectedItem as PeopleAge;
- Invalidate();
- }
- private void PlaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedPlace = (PlaceFilterComboBox.SelectedItem as PeoplePlace).title;
- Invalidate();
- }
- private bool sortAsc = true;
- private void RadioButton_Checked(object sender, RoutedEventArgs e)
- {
- sortAsc = (sender as RadioButton).Tag.ToString() == "1";
- Invalidate();
- }
- }
- public class LocalDataProvider : IDataProvider
- {
- public IEnumerable<PeopleGender> getGender()
- {
- return new PeopleGender[]
- {
- new PeopleGender()
- {
- title="Ж"
- },
- new PeopleGender()
- {
- title="М"
- }
- };
- }
- public IEnumerable<PeopleAge> getAge()
- {
- return new PeopleAge[]
- {
- new PeopleAge()
- {
- title="Все возраста",
- AgeFrom=0,
- AgeTo=99
- },
- new PeopleAge()
- {
- title="Подростки",
- AgeFrom=15,
- AgeTo=18
- },
- new PeopleAge()
- {
- title="Молодые",
- AgeFrom=18,
- AgeTo=30
- },
- new PeopleAge()
- {
- title="В Возврасте",
- AgeFrom=30,
- AgeTo=99
- }
- };
- }
- public IEnumerable<PeoplePlace> getPlace()
- {
- return new PeoplePlace[]
- {
- new PeoplePlace()
- {
- title="Набережная"
- },
- new PeoplePlace()
- {
- title="Йолка"
- },
- new PeoplePlace()
- {
- title="Бульвар"
- },
- new PeoplePlace()
- {
- title="ЙОТК"
- },
- new PeoplePlace()
- {
- title="Планета"
- },
- new PeoplePlace()
- {
- title="Ремзавод"
- },
- new PeoplePlace()
- {
- title="Медведево"
- },
- new PeoplePlace()
- {
- title="Пляж"
- },
- };
- }
- public IEnumerable<People> getPeople()
- {
- return new People[]{
- new People
- {
- Age=19,
- Name="Марина",
- Gender="Ж",
- Price=500,
- Place="Набережная"
- },
- new People
- {
- Price=9600,
- Name="Вика",
- Age=21,
- Gender="Ж",
- Place="Йолка"
- },
- new People
- {
- Price=50,
- Name="Кристина",
- Age=17,
- Gender="Ж",
- Place="Планета"
- },
- new People
- {
- Price=2000,
- Name="Анджелика",
- Age=16,
- Gender="Ж",
- Place="Бульвар"
- },
- new People
- {
- Price=1500,
- Name="Костя",
- Age=15,
- Gender="М",
- Place="Бульвар"
- },
- new People
- {
- Price=5000,
- Name="Вероника",
- Age=25,
- Gender="Ж",
- Place="Набережная"
- },
- new People
- {
- Price=1000,
- Name="Савелий",
- Age=20,
- Gender="М",
- Place="Пляж"
- },
- new People
- {
- Price=1500,
- Name="Александр",
- Age=39,
- Gender="М",
- Place="ЙОТК"
- },
- new People
- {
- Price=500,
- Name="Евгений",
- Age=18,
- Gender="М",
- Place="Медведево"
- },
- new People
- {
- Price=2500,
- Name="Давид",
- Age=18,
- Gender="М",
- Place="Ремзавод"
- },
- };
- }
- IEnumerable<models.People> IDataProvider.getPeople()
- {
- throw new NotImplementedException();
- }
- }
- public class JSONDataProvider : LocalDataProvider, IDataProvider
- {
- private List<People> _PeopleList;
- public JSONDataProvider()
- {
- var serializer = new DataContractJsonSerializer(typeof(People[]));
- using (var sr = new StreamReader("./data.json"))
- {
- _PeopleList = ((People[])serializer.ReadObject(sr.BaseStream)).ToList();
- }
- }
- public IEnumerable<People> getPeople()
- {
- return _PeopleList;
- }
- }
- }
|