dotnet / dotnet/maui

[Feature] Auto compiled binding on CollectionView's ItemTemplate, GroupHeaderTemplate and GroupFooterTemplate

Open
#8,161 6 comments 4 reactions 0 assignees View on GitHub
area-controls-collectionview area-xaml proposal/open
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

This feature determines automatically the `DataType` of a `DataTemplate` inside ``, `` and/or ``, without need of specifying `x:DataType` manually.
Also, it allows using an `x:DataType` on a page when there is Group Header/Footer Template that using group attributes.

## Normal CollectionView
For example, I have this `Monkey` model:
```csharp
public class Monkey
{
public string Species { get; set; }
}
```
Inside the ViewModel, I have this list of monkeys:
```csharp
public partial class MonkeysViewModel
{
public List MonkeysList { get; } = new();
}
```
And in the page, I have a `CollectionView` with a binding to the `MonkeysList`:
```xaml


```
Currently, in order to use the `ItemTemplate` without getting any binding error, I need to specify the type of the item using `x:DataType` on the `DataTemplate` tag, like this:
```xaml







```
### Feature benefits
This feature will make the code cleaner, will enable doing the same thing without specifying the `x:DataType`, and will automatically determine that because the `CollectionView`'s `ItemSource` is a Collection of `Monkey`'s, then each item is a `Monkey`.

## Grouped CollectionView
In a grouped `CollectionView`, the `ItemSource` needs to be an `IEnumerable` of `IEnumerable`s.
For example:
```csharp
public partial class MonkeysViewModel
{
public List> MonkeysGroups { get; } = new();
}
```
```xaml

```
If we want to use `` or ``, we can't use compiled binding on the page, because then it will require us to specify the `x:DataType` for the `DataTemplate`, which we can't do. For example:
```xaml












```
### Feature benefits
Besides the code being cleaner, this feature will allow using compiled binding on a page that has a grouped `CollectionView` in it, that uses the group's header and/or footer templates.

## Final code with the feature
```xaml












```

### Public API Changes

## Code
The example will use this code:
```csharp
public class Monkey
{
public string Species { get; set; }
}
```
```csharp
public partial class MonkeysViewModel
{
public List MonkeysList { get; } = new();
public List> MonkeysGroups { get; } = new();
}
```
## Normal CollectionView
For a non-grouped `CollectionView`, the xaml code, instead of this:
```xaml







```
Will look like this:
```xaml







```

## Grouped CollectionView
For a grouped `CollectionView`, the xaml code, instead of this:
```xaml












```
Will look like this:
```xaml












```

### Intended Use-Case

The feature will be used on any page that has a `CollectionView` in it and will make the code cleaner.
Also, it will enable to use compiled bindings on the page, even if it has a grouped `CollectionView` that uses group members inside the group header and/or footer templates.

Contributor guide

Open the contributing guide

Research direction

The issue names no source files, tests, or entry points. Start by searching for CollectionView.ItemTemplate, GroupHeaderTemplate, and GroupFooterTemplate and the compiled-binding XAML handling around them. Done means DataType is inferred for item, group-header, and group-footer templates, while page-level compiled bindings continue to work for grouped collections.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend, mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.