microsoft / microsoft/microsoft-ui-xaml
`ItemsPanelTemplate` doesn't support AoT-safe binding
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
We are encountering issues when trying to bind properties within an `ItemsPanelTemplate`. Currently, we have [this code](https://github.com/CommunityToolkit/Windows/blob/665ba24405f543242e5ff2b0d7d24c045d6836fb/components/Primitives/samples/StaggeredPanelSample.xaml#L42-L44) in our project:
```xml
```
This will crash at runtime when compiled under NativeAoT for both the WindowsAppSDK and UWP with modern .NET.
Removing the binding on the `ItemsPanelTemplate` fixes the issue, as shown in the image below:

Unfortunately, we cannot use `x:Bind` here because it results in the following error:
```
1>StaggeredPanelSample.xaml(42,22): XamlCompiler error WMC1111: DataTemplates containing x:Bind need a DataType to be specified using 'x:DataType'
1>Done building project \"Primitives.Samples.csproj\" -- FAILED.
```
Neither a `DataTemplate` nor its accompanying `x:DataType` can be added here since `ItemsPanelTemplate` is already a template and must have a first child of type `Panel`.
Looking through [documentation](https://github.com/MicrosoftDocs/windows-dev-docs/blob/docs/hub/apps/design/controls/item-templates-gridview.md) for 'official' examples of binding properties on an `ItemsPanelTemplate`, I'm not finding any where these properties are bound, whether using `{Binding}`, `{x:Bind}`, or `{TemplateBinding}`.
Using `{Binding}` worked before, but it's not AoT safe and `x:Bind` doesn't seem to be compatible. What is the platform recommended approach here?
Contributor guide
Research direction
Start with components/Primitives/samples/StaggeredPanelSample.xaml around lines 42-44 and reproduce the binding failure under NativeAoT for WindowsAppSDK or UWP. Compare the binding and x:Bind cases described in the issue; done means identifying and implementing or documenting a platform-supported AoT-safe approach for ItemsPanelTemplate bindings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100