Sem descrição

ebakhtin 0ca76310a0 first há 6 meses atrás
.vs c6654b1474 first há 6 meses atrás
Model c6654b1474 first há 6 meses atrás
assets c6654b1474 first há 6 meses atrás
bin c6654b1474 first há 6 meses atrás
img c6654b1474 first há 6 meses atrás
obj c6654b1474 first há 6 meses atrás
App.xaml c6654b1474 first há 6 meses atrás
App.xaml.cs c6654b1474 first há 6 meses atrás
AssemblyInfo.cs c6654b1474 first há 6 meses atrás
MainWindow.xaml c6654b1474 first há 6 meses atrás
MainWindow.xaml.cs c6654b1474 first há 6 meses atrás
readme.md 0ca76310a0 first há 6 meses atrás
wpf_filtering.csproj c6654b1474 first há 6 meses atrás
wpf_filtering.csproj.user c6654b1474 first há 6 meses atrás
wpf_filtering.sln c6654b1474 first há 6 meses atrás

readme.md

Каркас приложения. Модель данных. Привязка данных. Табличный вывод.

Файл класса:

    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;
    }
}

Результат: