tags:

views:

66

answers:

1

Greetings, I have a ViewModel for a ProductCategory. The ProductCategory has a boolean Active field.

Is is possible to have a single ProductCategoryViewModel and be able to get a collection of all ProductCategories and a collection of ACTIVE ProductCategories?

Or, would I have to create an ActiveProductCategoryViewModel?

I'm using MVVM-Light with RIA in Silverlight...so, I have a ProductCategory service with a GetProductCategories method and a GetActiveProductCategories method. I want to be able to get the ActiveProductCategories to populate a dropdown...but also get ALL the ProductCategories for maintenance and historical purposes etc.

Thanks! Butcher

+1  A: 

I assume you have another ViewModel with a collection of ProductCategoryViewModel objects? If so, I think its fine to have another collection of just the active product categories. I'm not sure you need a separate service method for this, since you can just filter your collection of product categories based on the Active value.

If this view model would be called ProductCategoriesViewModel, it might look like this:

using System.Collections.Generic;
using System.Linq;
using GalaSoft.MvvmLight;

namespace OCEAN.EPP.ViewModel
{
    public class ProductCategoriesViewModel : ViewModelBase
    {
        public ProductCategoriesViewModel()
        {
            if (IsInDesignMode)
            {
                ProductCategories = new List<ProductCategoryViewModel>
                {
                    new ProductCategoryViewModel { Active = false },
                    new ProductCategoryViewModel { Active = false },
                    new ProductCategoryViewModel { Active = true },
                    new ProductCategoryViewModel { Active = true },
                };
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
            }
        }

        public const string ProductCategoriesPropertyName = "ProductCategories";
        private List<ProductCategoryViewModel> _productCategories = new List<ProductCategoryViewModel>();
        public List<ProductCategoryViewModel> ProductCategories
        {
            get { return _productCategories; }
            set
            {
                if (_productCategories == value)
                    return;

                _productCategories = value;
                FilterActiveProductCategories();
                RaisePropertyChanged(ProductCategoriesPropertyName);
            }
        }

        public const string ActiveProductCategoriesPropertyName = "ActiveProductCategories";
        private List<ProductCategoryViewModel> _activeProductCategories = new List<ProductCategoryViewModel>();
        public List<ProductCategoryViewModel> ActiveProductCategories
        {
            get { return _activeProductCategories; }
            set
            {
                if (_activeProductCategories == value)
                    return;

                _activeProductCategories = value;
                RaisePropertyChanged(ActiveProductCategoriesPropertyName);
            }
        }

        private void FilterActiveProductCategories()
        {
            ActiveProductCategories = ProductCategories.Where(pc => pc.Active).ToList();
        }
    }
}
Matt Casto