managing-wpf-collectionview-mvvm

Encapsulates CollectionViewSource in a Service Layer to keep WPF ViewModels free of WPF dependencies.

Updated Nov 22, 2023
One-click install
npx skills add https://github.com/parksanghoon-sys/TestCode --skill managing-wpf-collectionview-mvvm-parksanghoon-sys
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: managing-wpf-collectionview-mvvm
Source: https://github.com/parksanghoon-sys/TestCode/tree/main/src/Modbus/wpf-dev-pack/.agents/skills/managing-wpf-collectionview-mvvm
Command: npx skills add https://github.com/parksanghoon-sys/TestCode --skill managing-wpf-collectionview-mvvm-parksanghoon-sys

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? WPF filtering, sorting, and grouping require CollectionViewSource and ICollectionView, which live in WPF assemblies. Referencing them directly in ViewModels breaks MVVM separation and makes ViewModels untestable. This Skill provides an architecture that encapsulates CollectionView access in a Service Layer so ViewModels only use pure BCL types like IEnumerable. ## Core Features & Use Cases - Service Layer Encapsulation: Wrap CollectionViewSource in a service that returns IEnumerable, keeping ViewModels free of WindowsBase and PresentationFramework references. - Filtering, Sorting, and Grouping: Create filtered, sorted, and grouped views from a single shared ObservableCollection, with XAML GroupStyle templates for ListBox and Expander-based grouping. - Framework Variants: Includes implementations for both CommunityToolkit.Mvvm (ObservableObject, source generators) and Prism 9 (BindableBase, IContainerRegistry). - Use Case: A single member collection must be displayed in multiple Views with different filter conditions (e.g., by device type). The service creates one filtered view per ViewModel while all Views stay synchronized with the shared source collection. ## Quick Start Ask the AI to implement a WPF MVVM architecture where CollectionView filtering is encapsulated in a Service Layer so the ViewModel only uses IEnumerable.

Frequently Asked Questions about managing-wpf-collectionview-mvvm

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I filter a collection in WPF without breaking MVVM?▼

Encapsulate CollectionViewSource in a Service Layer class that returns IEnumerable, and inject that service into the ViewModel. The ViewModel never references ICollectionView or WPF assemblies, so MVVM separation and testability are preserved.

How to add grouping to a WPF ListBox with CollectionView?▼

Add a PropertyGroupDescription to the view's GroupDescriptions in the Service Layer, then define a GroupStyle with a HeaderTemplate in the ListBox XAML. The header can bind to the group Name and ItemCount properties.

CommunityToolkit.Mvvm vs Prism 9 for WPF ViewModels?▼

CommunityToolkit.Mvvm uses ObservableObject with [ObservableProperty] source generators and Microsoft DI via IServiceCollection. Prism 9 uses BindableBase with manual SetProperty calls and registers types through IContainerRegistry. The Service Layer pattern works identically with both.

Can a ViewModel reference ICollectionView in strict MVVM?▼

No, ICollectionView lives in WindowsBase.dll, a WPF assembly. Referencing it couples the ViewModel to the UI framework and prevents unit testing without WPF. Use IEnumerable from the Service Layer instead for complete independence.

Why do multiple filtered views stay synchronized with one collection?▼

All views are created from the same underlying ObservableCollection source. When items are added or removed through the service, every CollectionView derived from that source automatically reflects the change through its own filter.