# Поиск, сортировка ## Class1 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class Client { public string Name { get; set; } public int Age { get; set; } public int Price { get; set; } public string Place { get; set; } public string Category { get; set; } public List ClientPriceList { get; set; } public List ClientCategoryList { get; set; } } } ``` ## Class2 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { class Globals { public static IDataProvider dataProvider; IEnumerable getClientPrice() { return new ClientPrice[] { new ClientPrice{title="Все цены", PriceFrom=500, PriceTo=30000}, new ClientPrice{title="Низкая цена", PriceFrom=500, PriceTo=3000}, new ClientPrice{title="Средняя цена", PriceFrom=3000, PriceTo = 15000}, new ClientPrice{title="Высокая цена", PriceFrom=15000, PriceTo=30000}, }; } IEnumerable getClientCategory() { return new ClientCategory[] { new ClientCategory { title = "По России" }, new ClientCategory { title = "За границей" }, }; } } } ``` ## Class3 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class ClientPrice { public string title { get; set; } public int PriceFrom { get; set; } public int PriceTo { get; set; } } public class ClientCategory { public string title { get; set; } } } ``` ## MainWindow.xaml.cs ``` using System.Collections.Generic; 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 WpfApp1.model; namespace WpfApp1 { public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void Invalidate() { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ClientList")); } public string selectedCategory = "Все категории"; public ClientPrice? selectedPrice = null; private IEnumerable _ClientList; public IEnumerable ClientList { get { var res = _ClientList; res = res .Where(c => (selectedPrice == null || (c.Price >= selectedPrice.PriceFrom && c.Price < selectedPrice.PriceTo))) .Where(c => (c.Category == selectedCategory || selectedCategory == "Все категории")); if (searchFilter != "") res = res.Where(c => c.Name.IndexOf( searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 || c.Category.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 { _ClientList = value; } } public List Client { get; set; } public List ClientCategoryList { get; set; } public List ClientPriceList { get; set; } public MainWindow() { { InitializeComponent(); DataContext = this; Globals.dataProvider = new LocalDataProvider(); ClientList = Globals.dataProvider.getClient(); ClientPriceList = Globals.dataProvider.getPrice().ToList(); ClientCategoryList = Globals.dataProvider.getCategory().ToList(); ClientCategoryList.Insert(0, new ClientCategory() { 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 ClientCategoryFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedCategory = (CategoryFilterComboBox.SelectedItem as ClientCategory).title; Invalidate(); } private void ClientPriceFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedPrice = PriceFilterComboBox.SelectedItem as ClientPrice; Invalidate(); } private bool sortAsc = true; private void RadioButton_Checked(object sender, RoutedEventArgs e) { sortAsc = (sender as RadioButton).Tag.ToString() == "1"; Invalidate(); } private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { } } interface IDataProvider { IEnumerable getClient(); IEnumerable getPrice(); IEnumerable getCategory(); } public class LocalDataProvider : IDataProvider { public IEnumerable getCategory() { return new ClientCategory[] { new ClientCategory() { title="По России" }, new ClientCategory() { title="За границей" }, }; } public IEnumerable getPrice() { return new ClientPrice[] { new ClientPrice() { title = "Все цены", PriceFrom = 500, PriceTo = 30000 }, new ClientPrice() { title = "Низкая цена", PriceFrom = 500, PriceTo = 3000 }, new ClientPrice() { title = "Средняя цена", PriceFrom = 3000, PriceTo = 15000 }, new ClientPrice() { title = "Высокая цена", PriceFrom = 15000, PriceTo = 30000 } }; } public IEnumerable getClient() { return new Client[]{ new Client{ Name = "Никита", Age = 20, Price=2300, Category="По России", Place="Москва"}, new Client{ Name = "Маша", Age = 15, Price=5000, Category="По России", Place="Сочи"}, new Client{ Name = "Алиса", Age = 35, Price=15000, Category="За границей", Place="Токио"}, new Client{ Name = "Леша", Age = 19, Price=20000, Category="За границей", Place="Южная Корея"}, new Client{ Name = "Андрей", Age = 28, Price=3000, Category="По России", Place="Крым"}, new Client{ Name = "Костя", Age = 14, Price=18000, Category="За границей", Place="Токио"}, new Client{ Name = "Даша", Age = 12, Price=25000, Category="За границей", Place="Лондон"}, new Client{ Name = "Ольга", Age = 55, Price=22000, Category="За границей", Place="Люксембург"}, new Client{ Name = "Катя", Age = 30, Price=1500, Category="По России", Place="Адлер"}, new Client{ Name = "Анжела", Age = 61, Price=25000, Category="За границей", Place="Мальдивы"}, new Client{ Name = "Миша", Age = 17, Price=30000, Category="За границей", Place="Дубаи"}, }; } } } ``` ## MainWindow.xaml ```