# Фильтрация данных ## Class1 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp8.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; using WpfApp8.model; namespace WpfApp8.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 WpfApp8.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.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 WpfApp8.model; namespace WpfApp8 { /// /// Interaction logic for MainWindow.xaml /// 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 { return _ClientList .Where(c => (selectedPrice == null || (c.Price >= selectedPrice.PriceFrom && c.Price < selectedPrice.PriceTo))) .Where(c => (c.Category == selectedCategory || selectedCategory == "Все категории")); } 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 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(); } } 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{ Price=2300, Category="По России", Name="Москва"}, new Client{ Price=5000, Category="По России", Name="Сочи"}, new Client{ Price=15000, Category="За границей", Name="Токио"}, new Client{ Price=20000, Category="За границей", Name="Южная Корея"}, new Client{ Price=3000, Category="По России", Name="Крым"}, new Client{ Price=25000, Category="За границей", Name="Лондон"}, new Client{ Price=22000, Category="За границей", Name="Люксембург"}, new Client{ Price=1500, Category="По России", Name="Адлер"}, new Client{ Price=25000, Category="За границей", Name="Мальдивы"}, new Client{ Price=30000, Category="За границей", Name="Дубаи"}, }; } } } ``` ## MainWindow.xaml ```