2010-07-16 2 views
1

인사말, ProductCategory에 대한 ViewModel이 있습니다. ProductCategory에 부울 활성 필드가 있습니다.ViewModel 빌드

단일 ProductCategoryViewModel을 보유하고 모든 ProductCategories 및 ACTIVE ProductCategories 컬렉션을 가져올 수 있습니까?

아니면 ActiveProductCategoryViewModel을 만들어야합니까?

Silverlight에서 RIA와 함께 MVVM-Light를 사용하고 있습니다 ... 그래서 GetProductCategories 메서드 및 GetActiveProductCategories 메서드가있는 ProductCategory 서비스가 있습니다. ActiveProductCategories를 가져 와서 드롭 다운을 채울 수 있기를 원하지만 유지 관리 및 역사적 목적을 위해 모든 ProductCategories를 가져오고 싶습니다.

감사합니다! 정육점

답변

1

ProductCategoryViewModel 개체 모음이있는 다른 ViewModel이 있다고 가정합니까? 그렇다면 적극적으로 제품 범주를 수집하는 것이 좋다고 생각합니다. 활성 값을 기준으로 제품 카테고리 모음을 필터링 할 수 있으므로 별도의 서비스 방법이 필요하지 않은지 잘 모르겠습니다. 이보기 모델이 ProductCategoriesViewModel를 호출 할 경우

, 그것은 다음과 같습니다

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