Valera a350517df1 first commit | 6 mēneši atpakaļ | |
---|---|---|
img | 6 mēneši atpakaļ | |
.gitignore.txt | 7 mēneši atpakaļ | |
readme.md | 6 mēneši atpakaļ |
"Вывод данных согласно макета (ListBox, Image)."
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<Namecar> 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<Carsprice> 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<CarWithStringDates> _CarList;
public JSONDataProvider()
{
using (var sr = new StreamReader("./Test.json"))
{
_CarList = JsonConvert.DeserializeObject<CarWithStringDates[]>(sr.ReadToEnd()).ToList();
}
}
public new IEnumerable<cars> 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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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<cars> _CarList;
public IEnumerable<cars> 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> Cars { get; set; }
public List<Namecar> CarinfoList { get; set; }
public List<Carsprice> 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<cars> getCars();
IEnumerable<Namecar> getName();
IEnumerable<Carsprice> getPrice();
}
public class LocalDataProvider : IDataProvider
{
public IEnumerable<Namecar> 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<Carsprice> 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<cars> 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
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Иванов Валера И-21" Height="450" Width="908">
<Window.Resources>
<BitmapImage
x:Key='defaultImage'
UriSource='./bin/Debug/net8.0-windows/img/net.jpg' />
</Window.Resources>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- лого -->
<Image
Source="./bin/Debug/net8.0-windows/img/net.jpg"
Grid.RowSpan="2" HorizontalAlignment="Right"/>
<ListBox
Grid.Row="1"
Grid.Column="1"
Background="White"
ItemsSource="{Binding CarList}">
<ListBox.ItemContainerStyle>
<Style
TargetType="ListBoxItem">
<Setter
Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border
BorderThickness="1"
BorderBrush="Black"
CornerRadius="5">
<Grid
Margin="10"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Image
Width="64"
Height="64"
Source="{Binding ImageBitmap}" />
<StackPanel
Grid.Column="1"
Margin="5"
Orientation="Vertical">
<TextBlock
Text="{Binding name}"/>
<TextBlock
Text="{Binding price}"/>
</StackPanel>
<TextBlock
Grid.Column="2"
Text="{Binding year}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel
Orientation="Vertical"
Grid.RowSpan="3"
VerticalAlignment="Bottom">
<Button
x:Name="ExitButton"
Content="Выход"
Click="ExitButton_Click"
Height="50"/>
</StackPanel>
<WrapPanel
Orientation="Horizontal"
Grid.Column="1"
MinHeight="50">
<Label
Content="Поиск"
VerticalAlignment="Center"/>
<TextBox
Width="200"
VerticalAlignment="Center"
x:Name="SearchFilterTextBox"
KeyUp="SearchFilter_KeyUp"/>
<Label
Content="Название:"
VerticalAlignment="Center"/>
<ComboBox
Name="carnameFilterComboBox"
SelectionChanged="carnameFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarinfoList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Цена:"
VerticalAlignment="Center"/>
<ComboBox
Name="carpriceFilterComboBox"
SelectionChanged="carpriceFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarpriceList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Возраст:"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="1"
Content="по возрастанию"
IsChecked="True"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="2"
Content="по убыванию"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
</WrapPanel>
</Grid>
</Window>
Вот что получилось
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Иванов Валера И-21" Height="450" Width="908">
<Window.Resources>
<BitmapImage
x:Key='defaultImage'
UriSource='./bin/Debug/net8.0-windows/img/net.jpg' />
</Window.Resources>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- лого -->
<Image
Source="./bin/Debug/net8.0-windows/img/net.jpg"
Grid.RowSpan="2" HorizontalAlignment="Right"/>
<ListBox
Grid.Row="1"
Grid.Column="1"
Background="White"
ItemsSource="{Binding CarList}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel
HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border
BorderThickness="1"
BorderBrush="Black"
CornerRadius="5">
<Grid
Margin="10"
Width="200"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Image
Width="64"
Height="64"
Source="{Binding ImageBitmap}" />
<StackPanel
Grid.Column="1"
Margin="5"
Orientation="Vertical">
<TextBlock
Text="{Binding name}"/>
<TextBlock
Text="{Binding price}"/>
</StackPanel>
<TextBlock
Grid.Column="2"
Text="{Binding year}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel
Orientation="Vertical"
Grid.RowSpan="3"
VerticalAlignment="Bottom">
<Button
x:Name="ExitButton"
Content="Выход"
Click="ExitButton_Click"
Height="50"/>
</StackPanel>
<WrapPanel
Orientation="Horizontal"
Grid.Column="1"
MinHeight="50">
<Label
Content="Поиск"
VerticalAlignment="Center"/>
<TextBox
Width="200"
VerticalAlignment="Center"
x:Name="SearchFilterTextBox"
KeyUp="SearchFilter_KeyUp"/>
<Label
Content="Название:"
VerticalAlignment="Center"/>
<ComboBox
Name="carnameFilterComboBox"
SelectionChanged="carnameFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarinfoList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Цена:"
VerticalAlignment="Center"/>
<ComboBox
Name="carpriceFilterComboBox"
SelectionChanged="carpriceFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarpriceList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Возраст:"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="1"
Content="по возрастанию"
IsChecked="True"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="2"
Content="по убыванию"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
</WrapPanel>
</Grid>
</Window>