Фильтрация данных
Class1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp8.model
{
public class Client
{
public string Name { get; set; }
public int Age { get; set; }
public int Price { get; set; }
public string Place { get; set; }
public string Category { get; set; }
public List<ClientPrice> ClientPriceList { get; set; }
public List<ClientCategory> ClientCategoryList { get; set; }
}
}
Class2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp8.model;
namespace WpfApp8.model
{
class Globals
{
public static IDataProvider dataProvider;
IEnumerable<ClientPrice> getClientPrice()
{
return new ClientPrice[] {
new ClientPrice{title="Все цены", PriceFrom=500, PriceTo=30000},
new ClientPrice{title="Низкая цена", PriceFrom=500, PriceTo=3000},
new ClientPrice{title="Средняя цена", PriceFrom=3000, PriceTo = 15000},
new ClientPrice{title="Высокая цена", PriceFrom=15000, PriceTo=30000},
};
}
IEnumerable<ClientCategory> getClientCategory()
{
return new ClientCategory[]
{
new ClientCategory { title = "По России" },
new ClientCategory { title = "За границей" },
};
}
}
}
Class3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp8.model
{
public class ClientPrice
{
public string title { get; set; }
public int PriceFrom { get; set; }
public int PriceTo { get; set;}
}
public class ClientCategory
{
public string title { get; set; }
}
}
MainWindow.xaml.cs
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 WpfApp8.model;
namespace WpfApp8
{
/// <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("ClientList"));
}
public string selectedCategory = "Все категории";
public ClientPrice? selectedPrice = null;
private IEnumerable<Client> _ClientList;
public IEnumerable<Client> ClientList
{
get
{
return _ClientList
.Where(c => (selectedPrice == null || (c.Price >= selectedPrice.PriceFrom && c.Price < selectedPrice.PriceTo)))
.Where(c => (c.Category == selectedCategory || selectedCategory == "Все категории"));
}
set
{
_ClientList = value;
}
}
public List<Client> Client { get; set; }
public List<ClientCategory> ClientCategoryList { get; set; }
public List<ClientPrice> ClientPriceList { get; set; }
public MainWindow()
{
{
InitializeComponent();
DataContext = this;
Globals.dataProvider = new LocalDataProvider();
ClientList = Globals.dataProvider.getClient();
ClientPriceList = Globals.dataProvider.getPrice().ToList();
ClientCategoryList=Globals.dataProvider.getCategory().ToList();
ClientCategoryList.Insert(0, new ClientCategory() { title = "Все категории" });
}
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void ClientCategoryFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedCategory = (CategoryFilterComboBox.SelectedItem as ClientCategory).title;
Invalidate();
}
private void ClientPriceFilterComboBox_selectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedPrice = PriceFilterComboBox.SelectedItem as ClientPrice;
Invalidate();
}
}
interface IDataProvider
{
IEnumerable<Client> getClient();
IEnumerable<ClientPrice> getPrice ();
IEnumerable<ClientCategory> getCategory();
}
public class LocalDataProvider : IDataProvider
{
public IEnumerable<ClientCategory> getCategory()
{
return new ClientCategory[]
{
new ClientCategory()
{
title="По России"
},
new ClientCategory()
{
title="За границей"
},
};
}
public IEnumerable<ClientPrice> getPrice()
{
return new ClientPrice[]
{
new ClientPrice()
{
title = "Все цены",
PriceFrom = 500,
PriceTo = 30000
},
new ClientPrice()
{
title = "Низкая цена",
PriceFrom = 500,
PriceTo = 3000
},
new ClientPrice()
{
title = "Средняя цена",
PriceFrom = 3000,
PriceTo = 15000
},
new ClientPrice()
{
title = "Высокая цена",
PriceFrom = 15000,
PriceTo = 30000
}
};
}
public IEnumerable<Client> getClient()
{
return new Client[]{
new Client{
Price=2300,
Category="По России",
Name="Москва"},
new Client{
Price=5000,
Category="По России",
Name="Сочи"},
new Client{
Price=15000,
Category="За границей",
Name="Токио"},
new Client{
Price=20000,
Category="За границей",
Name="Южная Корея"},
new Client{
Price=3000,
Category="По России",
Name="Крым"},
new Client{
Price=25000,
Category="За границей",
Name="Лондон"},
new Client{
Price=22000,
Category="За границей",
Name="Люксембург"},
new Client{
Price=1500,
Category="По России",
Name="Адлер"},
new Client{
Price=25000,
Category="За границей",
Name="Мальдивы"},
new Client{
Price=30000,
Category="За границей",
Name="Дубаи"},
};
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp8.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:WpfApp8"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- типа логотип компании -->
<Image
Source="/img/1.jpg"
Grid.RowSpan="2" HorizontalAlignment="Right"/>
<DataGrid
Grid.Row="1"
Grid.Column="1"
CanUserAddRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding ClientList}">
<DataGrid.Columns>
<DataGridTextColumn
Header="Место"
Binding="{Binding Name}"/>
<DataGridTextColumn
Header="Цена"
Binding="{Binding Price}"/>
<DataGridTextColumn
Header="Категория"
Binding="{Binding Category}"/>
</DataGrid.Columns>
</DataGrid>
<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"/>
<ComboBox
Name="CategoryFilterComboBox"
SelectionChanged="ClientCategoryFilterComboBox_selectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding ClientCategoryList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Ценовая категория:"
VerticalAlignment="Center"/>
<ComboBox
Name="PriceFilterComboBox"
SelectionChanged="ClientPriceFilterComboBox_selectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding ClientPriceList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<!-- минимальную высоту я тут поставил, чтобы верхнюю строку сетки было видно. В реальном приложении она не нужна -->
</WrapPanel>
</Grid>
</Window>