**["Вывод данных согласно макета (ListBox, Image)."](https://github.com/kolei/OAP/blob/master/articles/wpf_listbox.md)** *** **C#** *** **Class1** *** ``` using System; using WpfApp1; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { [DataContract] public class cars { [DataMember] public string name { get; set; } [DataMember] public int year { get; set; } [DataMember] public double price { get; set; } [DataMember] public string color { get; set; } [DataMember] public bool defects { get; set; } [DataMember] public DateOnly? dateOfLastSTO { get; set; } [DataMember] public string? photo { get; set; } public Uri? ImageBitmap { get { var imageName = Environment.CurrentDirectory + "/img/" + (photo ?? ""); return System.IO.File.Exists(imageName) ? new Uri(imageName) : null; } } } } ``` *** **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 getNamecar() { return new Namecar[] { new Namecar { title = "Toyota Trueno AE8"}, new Namecar { title = "Toyota Supra A80"}, new Namecar { title = "Nissal Skyline R34" }, new Namecar { title = "Nissan Silvia S15"}, new Namecar { title = "Toyota Camry 3.5"}, new Namecar { title = "Audi RS 6" }, new Namecar { title = "Трактор LOVOL TE354 HT"}, new Namecar { title = "BMW M5 F90"}, new Namecar { title = "BMW E36" }, new Namecar { title = "Daewoo Matiz" } }; } IEnumerable getCarsprice() { return new Carsprice[] { new Carsprice{title="Малая цена", priceFrom=0, priceTo=15001}, new Carsprice{title="Средняя цена", priceFrom = 20000, priceTo=35000}, new Carsprice{title="Высокая цена", priceFrom = 35001, priceTo=70000}, }; } } } ``` *** **Class3** *** ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class Carsprice { public string title { get; set; } public int priceFrom { get; set; } public int priceTo { get; set; } } public class Namecar { public string title { get; set; } } } ``` *** **Class4** *** ``` using Newtonsoft.Json; using System; using WpfApp1; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { public class JSONDataProvider : LocalDataProvider, IDataProvider { private List _CarList; public JSONDataProvider() { using (var sr = new StreamReader("./Test.json")) { _CarList = JsonConvert.DeserializeObject(sr.ReadToEnd()).ToList(); } } public new IEnumerable getCars() { return _CarList.Select(p => new cars { name = p.name, year = p.year, price = p.price, color = p.color, defects = p.defects, photo = p.photo, dateOfLastSTO = p.dateOfLastSTO == null ? null : DateOnly.Parse(p.dateOfLastSTO), }); } } } ``` *** **Class5** *** ``` using System; using WpfApp1; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace WpfApp1.model { [DataContract] public class CarWithStringDates : cars { [DataMember] public string? dateOfLastSTO { get; set; } } } ``` *** **Test.json** *** ``` [ { "name": "Toyota Trueno AE86", "year": 1995, "price": 15000, "color": "white", "defects": false, "dateOfLastSTO": "2024-12-05", "photo": "light2.jpg" }, { "name": "Toyota Supra A80", "year": 1996, "price": 30000, "color": "white", "defects": false, "dateOfLastSTO": "2023-10-02", "photo": "light3.jpg" }, { "name": "Nissal Skyline R34", "year": 1996, "price": 25000, "color": "gray", "defects": false, "dateOfLastSTO": "2024-11-05", "photo": "light4.jpg" }, { "name": "Toyota Camry 3.5", "year": 2020, "price": 20000, "color": "black", "defects": false, "dateOfLastSTO": "2022-12-10", "photo": "light5.jpg" }, { "name": "Audi RS 65", "year": 2016, "price": 30000, "color": "black", "defects": false, "dateOfLastSTO": "2023-12-07", "photo": "light6.jpg" }, { "name": "Трактор LOVOL TE354 HT", "year": 2024, "price": 66666, "color": "blue", "defects": false, "dateOfLastSTO": "2021-12-10", "photo": "light7.jpg" }, { "name": "BMW M5 F90", "year": 2019, "price": 30000, "color": "red", "defects": false, "dateOfLastSTO": "2024-04-10", "photo": "light8.jpg" }, { "name": "BMW E36", "year": 2006, "price": 35000, "color": "black", "defects": false, "dateOfLastSTO": "2019-12-17", "photo": "light9.jpg" }, { "name": "Daewoo Matiz", "year": 2010, "price": 20000, "color": "blue", "defects": false, "dateOfLastSTO": "2021-12-10", "photo": "light10.jpg" } ] ``` *** **MainWindow.xaml.cs** *** ``` using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using WpfApp1.model; namespace WpfApp1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window, INotifyPropertyChanged { private string searchFilter = ""; private void SearchFilter_KeyUp(object sender, KeyEventArgs e) { searchFilter = SearchFilterTextBox.Text; Invalidate(); } private bool sortAsc = true; private void RadioButton_Checked(object sender, RoutedEventArgs e) { sortAsc = (sender as RadioButton).Tag.ToString() == "1"; Invalidate(); } public event PropertyChangedEventHandler PropertyChanged; private void Invalidate() { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CarList")); } public string selectedname = "Все автомобили"; public Carsprice? selectedprice = null; private IEnumerable _CarList; public IEnumerable CarList { get { var res = _CarList; res = res .Where(c => (c.name == selectedname || selectedname == "Все автомобили")) .Where(c => (selectedprice == null || (c.price >= selectedprice.priceFrom && c.price < selectedprice.priceTo))); if (searchFilter != "") res = res.Where(c => c.name.IndexOf( searchFilter, StringComparison.OrdinalIgnoreCase) >= 0); if (sortAsc) res = res.OrderBy(c => c.year); else res = res.OrderByDescending(c => c.year); return res; } set { _CarList = value; } } public List Cars { get; set; } public List CarinfoList { get; set; } public List CarpriceList { get; set; } public MainWindow() { InitializeComponent(); DataContext = this; Globals.dataProvider = new JSONDataProvider(); CarList = Globals.dataProvider.getCars(); CarinfoList = Globals.dataProvider.getName().ToList(); CarinfoList.Insert(0, new Namecar { title = "Все автомобили" }); CarpriceList = Globals.dataProvider.getPrice().ToList(); } private void ExitButton_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void carnameFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedname = (carnameFilterComboBox.SelectedItem as Namecar).title; Invalidate(); } private void carpriceFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { selectedprice = carpriceFilterComboBox.SelectedItem as Carsprice; Invalidate(); } } interface IDataProvider { IEnumerable getCars(); IEnumerable getName(); IEnumerable getPrice(); } public class LocalDataProvider : IDataProvider { public IEnumerable getName() { return new Namecar[] { new Namecar() { title="Toyota Trueno AE86" }, new Namecar() { title="Toyota Supra A80" }, new Namecar() { title="Nissal Skyline R34" }, new Namecar() { title="Nissan Silvia S15" }, new Namecar() { title="Toyota Camry 3.5" }, new Namecar() { title="Audi RS 6" }, new Namecar() { title="Трактор LOVOL TE354 HT" }, new Namecar() { title="BMW M5 F90" }, new Namecar() { title="BMW E36" }, new Namecar() { title="Daewoo Matiz" } }; } public IEnumerable getPrice() { return new Carsprice[] { new Carsprice() { title="Все цены", priceFrom=0, priceTo=70000 }, new Carsprice() { title="Малая цена", priceFrom=0, priceTo=15001 }, new Carsprice() { title="Средняя цена", priceFrom=20000, priceTo=35000 }, new Carsprice() { title="Высокая цена", priceFrom=35001, priceTo=70000 } }; } public IEnumerable getCars() { return new cars[] { new cars { price = 15000, name = "Toyota Trueno AE86", year = 1995, }, new cars { price = 30000, name = "Toyota Supra A80", year = 1996, }, new cars { price = 25000, name = "Nissal Skyline R34", year = 1996 }, new cars { price = 20000, name = "Nissan Silvia S15", year = 1999, }, new cars { price = 20000, name = "Toyota Camry 3.5", year = 2020, }, new cars { price = 30000, name = "Audi RS 6", year = 2016, }, new cars { price = 66666, name = "Трактор LOVOL TE354 HT", year = 2024, }, new cars { price = 30000, name = "BMW M5 F90", year = 2019, }, new cars { price = 35000, name = "BMW E36", year = 2006, }, new cars { price = 20000, name = "Daewoo Matiz", year = 2010, }, }; } } } ``` *** **MainWindow.xaml** *** ```