Should Item Grouping/Filter be in the ViewModel or View layer?

I’m in a situation where I have a list of items that need to be displayed depending on their properties. What I’m unsure of is where is the best place to put the filtering/grouping logic of the viewmodel state?

Currently I have it in my view using converters, but I’m unsure whether I should have the logic in the viewmodel?

e.g.

ViewModel Layer:

class ItemViewModel
{
    DateTime LastAccessed { get; set; }
    bool IsActive { get; set; }
}

class ContainerViewModel
{
     ObservableCollection<Item> Items {get; set;}
}

View Layer:

<TextView Text="Active Items"/>
<List ItemsSource={Binding Items, Converter=GroupActiveItemsByDay}/>
<TextView Text="Active Items"/>
<List ItemsSource={Binding Items, Converter=GroupInActiveItemsByDay}/>

or should I build it like this?

ViewModel Layer:

class ContainerViewModel
{
   ObservableCollection<IGrouping<string, Item>> ActiveItemsByGroup {get; set;}
   ObservableCollection<IGrouping<string, Item>> InActiveItemsByGroup {get; set;}
}

View Layer:

<TextView Text="Active Items"/>
<List ItemsSource={Binding ActiveItemsGroupByDate }/>
<TextView Text="Active Items"/>
<List ItemsSource={Binding InActiveItemsGroupByDate }/>

Or maybe something in between?

ViewModel Layer:

class ContainerViewModel
{
   ObservableCollection<IGrouping<string, Item>> ActiveItems {get; set;}
   ObservableCollection<IGrouping<string, Item>> InActiveItems {get; set;}
}

View Layer:

<TextView Text="Active Items"/>
<List ItemsSource={Binding ActiveItems, Converter=GroupByDate }/>
<TextView Text="Active Items"/>
<List ItemsSource={Binding InActiveItems, Converter=GroupByDate }/>

I guess my question is what is good practice in terms as to what logic to put into the ViewModel and what logic to put into the Binding in the View, as they seem to overlap a bit?

Grouping and filtering is one of the explicit purposes of a View Model as opposed to just using a Model. The view model takes the data from the model and organizes it precisely for the given view.

That means where a model may often be a robust data representation, and the view only needs a subsection or aggregate of that model, the view model exists to give you the data your view needs so that your view does not need to do that leg work.

It can be confusing because often times people write code that has the view going straight to the model and in these cases the view does end up responsible for this work, but if you do use view models as an intermediary, you don’t need to and shouldn’t be doing this non-presentational work in the view.

2

I’m not sure there is a single correct answer here but I prefer to do the grouping in the XAML using a CollectionViewSource where I can. For example, something like the following might work for you:

<CollectionViewSource x:Key="groupedItems" Source="{Binding Items}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="IsActive"/>
        <PropertyGroupDescription PropertyName="LastAccessed"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

<ListBox ItemsSource="{Binding Source={StaticResource groupedItems}}"/>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật