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.Windows; using WpfApp1.model; using System.Threading.Tasks; using System; using WpfApp1.model; using WpfApp1.model; using WpfApp1.model; using System.IO; using System.Runtime.Serialization.Json; namespace WpfApp1 { /// /// 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("WorkerList")); } public string selectedPos = "Должность"; public WorkerAge? selectedAge = null; public string selectedSex = "Пол"; private IEnumerable _WorkerList; public IEnumerable WorkerList { get { var res = _WorkerList; res = res .Where(c => (c.Pos == selectedPos || selectedPos == "Должность")) .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo))) .Where(c => (c.Sex == selectedSex || selectedSex == "Пол")); 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 { _WorkerList = value; } } public List WorkerPosList { get; set; } public List WorkerAgeList { get; set; } public List WorkerSexList { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { Button clickedButton = (Button)sender; MessageBox.Show(clickedButton.Content.ToString()); } public MainWindow() { InitializeComponent(); DataContext = this; Globals.dataProvider = new JSONDataProvider(); WorkerList = Globals.dataProvider.getWorker(); WorkerPosList = Globals.dataProvider.getPos().ToList(); WorkerPosList.Insert(0, new WorkerPos { title = "Должность" }); WorkerAgeList = Globals.dataProvider.getAge().ToList(); WorkerSexList = Globals.dataProvider.getSex().ToList(); WorkerSexList.Insert(0, new WorkerSex { title = "Пол" }); WorkerList = Globals.dataProvider.getWorker(); Style buttonStyle = new Style(); buttonStyle.Setters.Add( new Setter { Property = Control.FontFamilyProperty, Value = new FontFamily("Verdana") }); buttonStyle.Setters.Add( new Setter { Property = Control.MarginProperty, Value = new Thickness(10) }); buttonStyle.Setters.Add( new Setter { Property = Control.BackgroundProperty, Value = new SolidColorBrush(Colors.LightCyan) }); buttonStyle.Setters.Add( new Setter { Property = Control.ForegroundProperty, Value = new SolidColorBrush(Colors.DarkSalmon) }); button1.Style = buttonStyle; button2.Style = buttonStyle; } public class JSONDataProvider : LocalDataProvider, IDataProvider { private List _WorkerList; public JSONDataProvider() { var serializer = new DataContractJsonSerializer(typeof(Worker[])); using (var sr = new StreamReader("./data.json")) { _WorkerList = ((Worker[])serializer.ReadObject(sr.BaseStream)).ToList(); } } public IEnumerable getWorker() { return _WorkerList; } } private bool sortAsc = true; private void ExitButton_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void PosFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedPos = (PosFilterComboBox.SelectedItem as WorkerPos).title; Invalidate(); } private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedAge = (AgeFilterComboBox.SelectedItem as WorkerAge); Invalidate(); } private void SexFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedSex = (SexFilterComboBox.SelectedItem as WorkerSex).title; Invalidate(); } private string searchFilter = ""; private void SearchFilter_KeyUp(object sender, KeyEventArgs e) { searchFilter = SearchFilterTextBox.Text; Invalidate(); } private void RadioButton_Checked(object sender, RoutedEventArgs e) { sortAsc = (sender as RadioButton).Tag.ToString() == "1"; Invalidate(); } private void button2_Click(object sender, RoutedEventArgs e) { } private void WorkerListBox_MouseDoubleClick( object sender, MouseButtonEventArgs e) { var detailWindow = new DetailWindow( WorkerListBox.SelectedItem as Worker); detailWindow.ShowDialog(); } } interface IDataProvider { IEnumerable getWorker(); IEnumerable getPos(); IEnumerable getAge(); IEnumerable getSex(); } public class LocalDataProvider : IDataProvider { public IEnumerable getPos() { return new WorkerPos[] { new WorkerPos() { title="Директор" }, new WorkerPos() { title="Продавец" }, new WorkerPos() { title="Механик" }, new WorkerPos() { title="Уборщик" }, new WorkerPos() { title="Стажер" }, new WorkerPos() { title="Оператор" }, }; } public IEnumerable getAge() { return new WorkerAge[] { new WorkerAge() { title="Все возраста", AgeFrom=0, AgeTo=110 }, new WorkerAge() { title="Дети", AgeFrom=1, AgeTo=6 }, new WorkerAge() { title="Подростки", AgeFrom=7, AgeTo=21 }, new WorkerAge() { title="Молодые", AgeFrom=22, AgeTo=50 }, new WorkerAge() { title="Старики", AgeFrom=51, AgeTo=110 } }; } public IEnumerable getSex() { return new WorkerSex[] { new WorkerSex() { title="Человек" }, new WorkerSex() { title="Женщина" } }; } public IEnumerable getWorker() { return new Worker[]{ new Worker{ Sex="Человек", name="Вася", Age=25, Pos="Директор"}, new Worker{ Sex="Человек", name="Петя", Age=45, Pos="Продавец"}, new Worker{ Sex="Человек", name="Степа", Age = 30, Pos="Механик"}, new Worker{ Sex="Человек", name="Миша", Age = 100, Pos="Стажер"}, new Worker{ Sex="Женщина", name="Лена", Age = 12, Pos="Оператор"} }; } } }