ababin 7bb3734ee5 Обновить 'README.md' | 5 tháng trước cách đây | |
---|---|---|
.vs | 5 tháng trước cách đây | |
WpfApp2 | 5 tháng trước cách đây | |
img | 5 tháng trước cách đây | |
README.md | 5 tháng trước cách đây | |
WpfApp2.sln | 5 tháng trước cách đây |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp2
{
public partial class MainWindow : Window
{
private List<MusicalInstrument> instruments;
private Dictionary<string, Func<MusicalInstrument, bool>> filters;
public MainWindow()
{
InitializeComponent();
InitializeData();
foreach (var filter in filters.Keys)
{
FilterComboBox.Items.Add(filter);
}
UpdateInstrumentList(instruments);
}
private void InitializeData()
{
instruments = new List<MusicalInstrument>
{
new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 },
new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 },
new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 },
new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 },
new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },,
};
filters = new Dictionary<string, Func<MusicalInstrument, bool>>
{
{ "Все", i => true },
{ "Струнные", i => i.Type == "Струнные" },
{ "Ударные", i => i.Type == "Ударные" },
{ "Клавишные", i => i.Type == "Клавишные" },
{ "Медные", i => i.Type == "Медные" },
{ "Деревянные", i => i.Type == "Деревянные" },
{ "До 500", i => i.Price <= 500 },
{ "От 500 до 1000", i => i.Price > 500 && i.Price <= 1000 },
{ "Более 1000", i => i.Price > 1000 }
};
}
private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedFilter = (string)FilterComboBox.SelectedItem;
if (filters.TryGetValue(selectedFilter, out Func<MusicalInstrument, bool> filterFunc))
{
List<MusicalInstrument> filteredInstruments = instruments.Where(filterFunc).ToList();
UpdateInstrumentList(filteredInstruments);
}
}
private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
{
InstrumentListView.ItemsSource = instrumentsToDisplay;
}
}
class MusicalInstrument
{
public string Name { get; set; }
public string Type { get; set; }
public double Price { get; set; }
}
}
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="Musical Store" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox x:Name="FilterComboBox" SelectionChanged="FilterComboBox_SelectionChanged" Margin="10" />
<ListView x:Name="InstrumentListView" Grid.Row="1" Margin="10">
<ListView.View>
<GridView>
<GridViewColumn Header="Название" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Тип" DisplayMemberBinding="{Binding Type}" />
<GridViewColumn Header="Цена" DisplayMemberBinding="{Binding Price}" />
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp2
{
public partial class MainWindow : Window
{
private List<MusicalInstrument> instruments;
public MainWindow()
{
InitializeComponent();
InitializeData();
FilterComboBox.Items.Add("Все");
FilterComboBox.Items.Add("Клавишные");
FilterComboBox.Items.Add("Струнные");
FilterComboBox.Items.Add("Ударные");
FilterComboBox.SelectedIndex = 0;
UpdateInstrumentList(instruments);
}
private void InitializeData()
{
instruments = new List<MusicalInstrument>
{
new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 },
new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 },
new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 },
new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 },
new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },
};
}
private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedFilter = (string)FilterComboBox.SelectedItem;
List<MusicalInstrument> filteredInstruments = new List<MusicalInstrument>();
switch (selectedFilter)
{
case "Все":
filteredInstruments = instruments;
break;
case "Клавишные":
filteredInstruments = instruments.Where(i => i.Type == "Клавишные").ToList();
break;
case "Струнные":
filteredInstruments = instruments.Where(i => i.Type == "Струнные").ToList();
break;
case "Ударные":
filteredInstruments = instruments.Where(i => i.Type == "Ударные").ToList();
break;
}
UpdateInstrumentList(filteredInstruments);
}
private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
{
InstrumentListView.ItemsSource = instrumentsToDisplay;
}
}
class MusicalInstrument
{
public string Name { get; set; }
public string Type { get; set; }
public string Brand { get; set; }
public double Price { get; set; }
}
}
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="Musical Store" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10">
<Label Content="Фильтр:" VerticalAlignment="Center" />
<ComboBox x:Name="FilterComboBox" SelectionChanged="FilterComboBox_SelectionChanged" Margin="10" />
</StackPanel>
<ListView x:Name="InstrumentListView" Grid.Row="1" Margin="10">
<ListView.View>
<GridView>
<GridViewColumn Header="Название" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Тип" DisplayMemberBinding="{Binding Type}" />
<GridViewColumn Header="Бренд" DisplayMemberBinding="{Binding Brand}" />
<GridViewColumn Header="Цена" DisplayMemberBinding="{Binding Price}" />
</ListView.View>
</ListView>
</Grid>
</Window>