Encapsulates CollectionView in Service Layer to maintain MVVM principles in WPF. Use when implementing filtering, sorting, or grouping while keeping ViewModels free of WPF dependencies.
View on GitHubchristian289/dotnet-with-claudecode
wpf-dev-pack
wpf-dev-pack/skills/managing-wpf-collectionview-mvvm/SKILL.md
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/managing-wpf-collectionview-mvvm/SKILL.md -a claude-code --skill managing-wpf-collectionview-mvvmInstallation paths:
.claude/skills/managing-wpf-collectionview-mvvm/# 5.6 MVVM Pattern with CollectionView
## Project Structure
The templates folder contains a .NET 9 WPF project example.
```
templates/
├── WpfCollectionViewSample.Core/ ← Pure C# models and interfaces
│ ├── Member.cs
│ ├── IMemberCollectionService.cs
│ └── WpfCollectionViewSample.Core.csproj
├── WpfCollectionViewSample.ViewModels/ ← ViewModel (no WPF references)
│ ├── MainViewModel.cs
│ ├── GlobalUsings.cs
│ └── WpfCollectionViewSample.ViewModels.csproj
├── WpfCollectionViewSample.WpfServices/ ← WPF Service Layer
│ ├── MemberCollectionService.cs
│ ├── GlobalUsings.cs
│ └── WpfCollectionViewSample.WpfServices.csproj
└── WpfCollectionViewSample.App/ ← WPF Application
├── Views/
│ ├── MainWindow.xaml
│ └── MainWindow.xaml.cs
├── App.xaml
├── App.xaml.cs
├── GlobalUsings.cs
└── WpfCollectionViewSample.App.csproj
```
#### 5.6.1 Problem Scenario
When a single source collection needs to be filtered with different conditions across multiple Views while adhering to MVVM principles
#### 5.6.2 Core Principles
- **ViewModel must not reference WPF-related assemblies** (MVVM violation)
- **Encapsulate `CollectionViewSource` access through Service Layer**
- **ViewModel uses only `IEnumerable` or pure BCL types**
#### 5.6.3 Architecture Layer Structure
```
View (XAML)
↓ DataBinding
ViewModel Layer (uses IEnumerable, no WPF assembly reference)
↓ IEnumerable interface
Service Layer (uses CollectionViewSource directly)
↓
Data Layer (ObservableCollection<T>)
```
#### 5.6.4 Implementation Pattern
**1. Service Layer (CollectionViewFactory/Store)**
```csharp
// Services/MemberCollectionService.cs
// This class can reference PresentationFramework
namespace MyApp.Services;
public sealed class MemberCollectionService
{
private ObservableCollection<Member> Source { get; } = [];
// Factory Method: Create filtered view
// Returns IEnumerable so ViewModel doesn't know WPF types