123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
-
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using WpfApp2.model;
- using System.IO;
- using System.Runtime.Serialization.Json;
- using System.Windows.Media;
- using WpfApp2.Windows;
- namespace WpfApp2
- {
- /// <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("SparesList"));
- }
- public string selectedDetail = "Деталь";
- public SparesPrice? selectedPrice = null;
- public string selectedType = "Тип ТС";
- private IEnumerable<Spares> _SparesList;
- public IEnumerable<Spares> SparesList
- {
- get
- {
- var res = _SparesList;
- res = res
- .Where(c => (c.detail == selectedDetail || selectedDetail == "Деталь"))
- .Where(c => (selectedPrice == null || (c.price >= selectedPrice.PriceFrom && c.price < selectedPrice.PriceTo)))
- .Where(c => (c.type == selectedType || selectedType == "Тип ТС"));
- if (searchFilter != "")
- res = res.Where(c => c.name.IndexOf(
- searchFilter,
- StringComparison.OrdinalIgnoreCase) >= 0);
- if (sortAsc) res = res.OrderBy(c => c.price);
- else res = res.OrderByDescending(c => c.price);
- return res;
- }
- set
- {
- _SparesList = value;
- }
- }
- public List<SparesDetail> SparesDetailList { get; set; }
- public List<SparesPrice> SparesPriceList { get; set; }
- public List<SparesType> SparesTypeList { get; set; }
- public MainWindow()
- {
- { {
- InitializeComponent();
- DataContext = this;
- Globals.dataProvider = new JSONDataProvider();
- SparesList = Globals.dataProvider.getSpares();
- SparesDetailList = Globals.dataProvider.getDetail().ToList();
- SparesDetailList.Insert(0, new SparesDetail { title = "Деталь" });
- SparesPriceList = Globals.dataProvider.getPrice().ToList();
- SparesTypeList = Globals.dataProvider.getType().ToList();
- SparesTypeList.Insert(0, new SparesType { title = "Тип ТС" });
-
- 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.DarkOliveGreen)
- });
- buttonStyle.Setters.Add(
- new Setter
- {
- Property = Control.ForegroundProperty,
- Value = new SolidColorBrush(Colors.HotPink)
- });
- button1.Style = buttonStyle;
- button2.Style = buttonStyle;
- }
- }
- }
- public class JSONDataProvider : LocalDataProvider, IDataProvider
- {
- private List<Spares> _SparesList;
- public JSONDataProvider()
- {
- var serializer = new DataContractJsonSerializer(typeof(Spares[]));
- using (var sr = new StreamReader("./data.json"))
- {
- _SparesList = ((Spares[])serializer.ReadObject(sr.BaseStream)).ToList();
- }
- }
- public IEnumerable<Spares> getSpares()
- {
- return _SparesList;
- }
- }
- private bool sortAsc = true;
- private void RadioButton_Checked(object sender, RoutedEventArgs e)
- {
- sortAsc = (sender as RadioButton).Tag.ToString() == "1";
- Invalidate();
- }
- private void ExitButton_Click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- private void DetailFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedDetail = (DetailFilterComboBox.SelectedItem as SparesDetail).title;
- Invalidate();
- }
- private void PriceFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedPrice = PriceFilterComboBox.SelectedItem as SparesPrice;
- Invalidate();
- }
-
- private void TypeFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- selectedType = (TypeFilterComboBox.SelectedItem as SparesType).title;
- Invalidate();
- }
- private string searchFilter = "";
- private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
- {
- searchFilter = SearchFilterTextBox.Text;
- Invalidate();
- }
- private void SparesListBox_MouseDoubleClick(
- object sender,
- MouseButtonEventArgs e)
- {
- var detailWindow = new DetailWindow(
- SparesListBox.SelectedItem as Spares);
- detailWindow.ShowDialog();
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- Button clickedButton = (Button)sender;
- MessageBox.Show(clickedButton.Content.ToString());
- }
- }
- interface IDataProvider
- {
- IEnumerable<Spares> getSpares();
- IEnumerable<SparesDetail> getDetail();
- IEnumerable<SparesPrice> getPrice();
- IEnumerable<SparesType> getType();
- }
- public class LocalDataProvider : IDataProvider
- {
- public IEnumerable<SparesDetail> getDetail()
- {
- return new SparesDetail[]
- {
- new SparesDetail()
- {
- title="Двигатель"
- },
- new SparesDetail()
- {
- title="КПП"
- },
- new SparesDetail()
- {
- title="Подвеска"
- },
- new SparesDetail()
- {
- title="Стекла"
- },
- new SparesDetail()
- {
- title="Колеса"
- },
- };
- }
- public IEnumerable<SparesPrice> getPrice()
- {
- return new SparesPrice[]
- {
- new SparesPrice()
- {
- title="Все цены",
- PriceFrom=0,
- PriceTo=99999999
- },
- new SparesPrice()
- {
- title="БУ",
- PriceFrom=0,
- PriceTo=10000
- },
- new SparesPrice()
- {
- title="Дешевая",
- PriceFrom=10000,
- PriceTo=20000
- },
- new SparesPrice()
- {
- title="Средняя",
- PriceFrom=20000,
- PriceTo=50000
- },
- new SparesPrice()
- {
- title="Элитная",
- PriceFrom=50000,
- PriceTo=99999999
- }
- };
- }
- public IEnumerable<SparesType> getType()
- {
- return new SparesType[]
- {
- new SparesType()
- {
- title="Легкая"
- },
- new SparesType()
- {
- title="Грузовая"
- },
- };
- }
- public IEnumerable<Spares> getSpares()
- {
- return new Spares[]{
- new Spares{
- type="Легкая",
- name="Двигатель МБ140(ОК2281.4)",
- price = 72000,
- detail="Двигатель"},
- new Spares{
- type="Легкая",
- name="КПП 5-СТ РС-ПДР",
- price = 20000,
- detail="КПП"},
- new Spares{
- type="Грузовая",
- name="Подвеска ПДРС1337",
- price = 15000,
- detail="Подвеска"},
- new Spares{
- type="Легкая",
- name="Шина 15-Р",
- price = 3000,
- detail="Колеса"},
- new Spares{
- type="Грузовая",
- name="Лобовое стекло СТ150",
- price = 40000,
- detail="Стекла"},
- };
- }
- }
- }
|