public ProductListingViewModel() { CategoryId = "1"; CategoryName = "CategoryName"; PivotItems = new List <SubCategoryDisplayItem>(); if (DesignMode.DesignModeEnabled) { //Generate Fake Design Time Data. foreach (var subCategory in DesignTimeDataService.GenerateAudioSubCategories()) { var sc = new SubCategoryDisplayItem("1", "Audio", subCategory); var prds = new List <Product>(); prds.AddRange(DesignTimeDataService.GenerateFavoriteProducts(9, "Fabrikam")); prds.AddRange(DesignTimeDataService.GenerateFavoriteProducts(4, "Contoso")); prds.AddRange(DesignTimeDataService.GenerateFavoriteProducts(7, "Proseware")); sc.AllProducts = new List <Product>(prds); sc.FilteredProducts = new List <Product>(prds); sc.CurrentFilters.Add(new ProductListFilterItem("$222 - $333", "Price Range", false)); sc.CurrentFilters.Add(new ProductListFilterItem("32", "Megabytes", false)); sc.CurrentFilters.Add(new ProductListFilterItem("5x", "Optical Zoom", false)); if (PivotItems.Count == 0) { SelectedPivot = sc; } PivotItems.Add(sc); } FavoriteItems = DesignTimeDataService.GenerateFavoriteProducts(4); HasInitialized = true; } }
public override async Task InitializeViewModel(string param) { if (HasInitialized) { return; } ProductCategory category = await DataManager.CategoryDataSource.GetById(param); List <ProductSubCategory> subCategories = null; ProductSubCategory selectedSubCategory = null; if (category == null) { //Not a Category - must be a subcategory category = await DataManager.CategoryDataSource.GetParentCategory(param); if (category != null) { subCategories = category.SubCategoryItems; selectedSubCategory = subCategories.FirstOrDefault(item => item.Id == param); } else { //error - id is not a category or subcategory throw new Exception("The Parameter is not a Category or a SubCategory"); } } else { //Populate the SubCategories + get the first SubCategory; subCategories = category.SubCategoryItems; selectedSubCategory = subCategories.FirstOrDefault(); } CategoryId = category.Id; //Populate the SubCategory Display Items.. ObservableCollection <SubCategoryDisplayItem> subCategoryDisplayItems = new ObservableCollection <SubCategoryDisplayItem>(); SubCategoryDisplayItem selectedSubCategoryDisplayItem = null; foreach (var subCategory in subCategories) { var sc = new SubCategoryDisplayItem(category.Id, category.Name, subCategory); var prds = (await DataManager.ProductDataSource.GetProductsBySubCategoryIdAsync(subCategory.Id)).ToList(); sc.AllProducts = new List <Product>(prds); sc.FilteredProducts = new List <Product>(prds); subCategoryDisplayItems.Add(sc); if (selectedSubCategory.Id == sc.Id) { selectedSubCategoryDisplayItem = sc; } } CategoryName = category.Name; CategoryId = category.Id; SelectedPivot = selectedSubCategoryDisplayItem; PivotItems = subCategoryDisplayItems.ToList(); }
public static async void ShowProductFilterFlyout(SubCategoryDisplayItem subcategoryDisplayItem, ProductListFilterViewModel fvm) { try { //Check if we are on Windows Mobile bool isWindowsPhone = DeviceFamilyHelper.CurrentDeviceFamily() == DeviceFamily.Mobile; //If Windows Phone then use the Dialog Control otherwise use the SettingsFlyout control. if (isWindowsPhone) { ProductListFilterFlyout flyout = new ProductListFilterFlyout() { DataContext = fvm, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; flyout.Margin = new Thickness(0, 20, 0, 0); ContentDialog dialog = new ContentDialog() { Title = "Select Product Filters", Content = flyout, PrimaryButtonText = "Close", FullSizeDesired = true, Background = (SolidColorBrush)Application.Current.Resources["FlyoutBackgroundThemeBrush"], Foreground = new SolidColorBrush(Colors.White), RequestedTheme = ElementTheme.Dark, VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch }; await dialog.ShowAsync(); } else { SettingsFlyout fly = new SettingsFlyout(); ProductListFilterFlyout flyout = new ProductListFilterFlyout() { DataContext = fvm }; //(subcategoryDisplayItem); fly.Content = flyout; fly.RequestedTheme = ElementTheme.Dark; fly.Background = (SolidColorBrush)Application.Current.Resources["FlyoutBackgroundThemeBrush"]; fly.HeaderBackground = (SolidColorBrush)Application.Current.Resources["FlyoutBackgroundThemeBrush"]; fly.Title = "Filter Products"; fly.ShowIndependent(); } } catch (Exception ex) { string error = ex.Message; } }
//Initialize the ViewModel Filter Groups via referenced SubCategoryDisplayItem public void InitializeFilterGroupsFromProductList(SubCategoryDisplayItem subCategoryDisplayItem) { _subCategoryDisplayItem = subCategoryDisplayItem; // Build our list of Filter Groups _filterGroups.Clear(); if (_subCategoryDisplayItem?.AllProducts != null) { if (_subCategoryDisplayItem.AllProducts.Any()) { foreach (var groupItem in BuildFilterGroups(this, _subCategoryDisplayItem.AllProducts.ToList())) { // Add EventHandler so we can react to filter changes (and notify parent page) groupItem.FilterUpdated += GroupItem_FilterUpdated; _filterGroups.Add(groupItem); } } } }