ebakhtin 0ca76310a0 first | 6 months ago | |
---|---|---|
.vs | 6 months ago | |
Model | 6 months ago | |
assets | 6 months ago | |
bin | 6 months ago | |
img | 6 months ago | |
obj | 6 months ago | |
App.xaml | 6 months ago | |
App.xaml.cs | 6 months ago | |
AssemblyInfo.cs | 6 months ago | |
MainWindow.xaml | 6 months ago | |
MainWindow.xaml.cs | 6 months ago | |
readme.md | 6 months ago | |
wpf_filtering.csproj | 6 months ago | |
wpf_filtering.csproj.user | 6 months ago | |
wpf_filtering.sln | 6 months ago |
Файл класса:
public class ProductCategory
{
public string title { get; set; }
}
public class ProductPrice
{
public string title { get; set; }
public int priceFrom { get; set; }
public int priceTo { get; set; }
}
public class ProductCompany
{
public string title { get; set; }
}
interface IDataProvider
{
IEnumerable<Product> GetProducts();
IEnumerable<ProductCategory> GetCategories();
IEnumerable<ProductPrice> GetPrices();
IEnumerable<ProductCompany> GetCompanies();
}
public IEnumerable<ProductCategory> GetCategories()
{
return new ProductCategory[]
{
new ProductCategory {title = "Смартфон"},
new ProductCategory {title = "Ноутбук"},
new ProductCategory {title = "Смарт-Часы"},
new ProductCategory {title = "Монитор"}
};
}
public IEnumerable<ProductPrice> GetPrices()
{
return new ProductPrice[]
{
new ProductPrice{title="Все цены", priceFrom = 0, priceTo = 999999},
new ProductPrice{title="До 10.000", priceFrom = 0, priceTo = 10000},
new ProductPrice{title="От 10.000 до 20.000", priceFrom = 10000, priceTo = 20000},
new ProductPrice{title="От 20.000 до 50.000", priceFrom = 20000, priceTo = 50000},
new ProductPrice{title="Дороже 50.000", priceFrom = 50000, priceTo = 999999 }
};
}
public IEnumerable<ProductCompany> GetCompanies()
{
return new ProductCompany[]
{
new ProductCompany{title = "Apple"},
new ProductCompany{title = "Xiaomi"},
new ProductCompany{title = "Huawei"},
new ProductCompany{title = "POCO"},
new ProductCompany{title = "Asus"},
new ProductCompany{title = "Lenovo"},
new ProductCompany{title = "Samsung"}
};
}
XAML:
<ComboBox
Name="CategoryFilterComboBox"
SelectionChanged="CategoryFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CategoryList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Цена"/>
<ComboBox
Name ="PriceFilterComboBox"
SelectionChanged="PriceFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding ProductPrices}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Компания"/>
<ComboBox
Name ="CompanyFilterComboBox"
SelectionChanged="CompanyFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding ProductCompanies}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private IEnumerable<Product> _productList;
public IEnumerable<Product> ProductList
{
get
{
return _productList
.Where(p =>(selectedCategory == "Все категории" || p.Category == selectedCategory))
.Where(p =>(selectedPrice == null || (p.Price>=selectedPrice.priceFrom && p.Price<selectedPrice.priceTo)))
.Where(p =>(selectedCompany == "Все компании" || p.Company == selectedCompany));
}
set
{
_productList = value;
}
}
public List <ProductCategory> CategoryList { get; set; }
public List <ProductPrice> ProductPrices { get; set; }
public List <ProductCompany> ProductCompanies { get; set; }
string selectedCategory = "";
string selectedCompany = "";
private ProductPrice? selectedPrice = null;
public MainWindow()
{
InitializeComponent();
DataContext = this;
Globals.dataProvider = new LocalDataProvider();
ProductList = Globals.dataProvider.GetProducts();
ProductPrices = Globals.dataProvider.GetPrices().ToList();
CategoryList = Globals.dataProvider.GetCategories().ToList();
CategoryList.Insert(0, new ProductCategory { title = "Все категории" });
ProductCompanies = Globals.dataProvider.GetCompanies().ToList();
ProductCompanies.Insert(0, new ProductCompany { title = "Все компании" });
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void CategoryFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedCategory = (CategoryFilterComboBox.SelectedItem as ProductCategory).title;
Invalidate();
}
private void PriceFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedPrice = PriceFilterComboBox.SelectedItem as ProductPrice;
Invalidate();
}
private void CompanyFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedCompany = (CompanyFilterComboBox.SelectedItem as ProductCompany).title;
Invalidate();
}
public event PropertyChangedEventHandler PropertyChanged;
private void Invalidate()
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ProductList"));
}
}
class Globals
{
public static IDataProvider dataProvider;
}
}