Skip to content

Create DataTemplates in code with the new DataTemplate(DataTemplateElementFactory) constructor - #2258

Merged
Niels Laute (niels9001) merged 1 commit into
mainfrom
niels9001-winappsdk-experimental-code-apis
Oct 7, 2026
Merged

Niels Laute (niels9001) merged 1 commit into
mainfrom
niels9001-winappsdk-experimental-code-apis

Conversation

@niels9001

Copy link
Copy Markdown
Collaborator

Description

Adopts the experimental DataTemplate(DataTemplateElementFactory) constructor added in Windows App SDK 2.5.4-experimental (the version the gallery already targets).

  • MenuItemTemplateSelector: replaces the XamlReader.Load string templates with new DataTemplate(() => ...) factories. This removes runtime XAML parsing.
  • NavigationView, "Data binding" sample: adds a Separator and a Header ("More categories") to the bound items. Before, the selector's header and separator templates were never used. Header is now marked [WinRT.GeneratedBindableCustomProperty] so its {Binding Name} works under AOT. The shown C# snippet (NavigationViewDataBinding.txt) is updated to match.
  • Templates page: new example "Create a DataTemplate in code (experimental)". It shows a ComboBox whose ItemTemplate is built in code-behind (FontIcon + bound TextBlock), with XAML/C# snippets in TemplatesCreateDatatemplateInCode.txt.
  • Adds the DataTemplateElementFactory tag to the Templates entry in ControlInfoData.json and regenerates catalog/windows-samples.json.

FrameworkElement.SetThemeResourceBinding, the other new API in this release, is covered in a separate PR.

Motivation and Context

Before this release, a DataTemplate could only be created in code by building a XAML string and calling XamlReader.Load. The new constructor takes a factory delegate, which is type-safe and avoids parsing XAML. The gallery should show this pattern and use it itself.

How Has This Been Tested?

  • msbuild WinUIGallery\WinUIGallery.csproj /restore /p:Configuration=Debug /p:Platform=x64 /m: succeeded (no new warnings).
  • msbuild WinUIGallery\WinUIGallery.csproj /restore /p:Configuration=Debug-Unpackaged /p:Platform=x64 /m: succeeded.
  • dotnet run --project tools/CatalogExporter -- generate: succeeded (125 controls, 342 samples).
  • dotnet test tests\WinUIGallery.CatalogExporter.Tests: passed (159/159).
  • Manual check, Debug-Unpackaged x64 app:
    • Templates page: the new ComboBox shows the code-built template (star icon + "Option 1").
    • NavigationView "Data binding" sample: shows Category 1–2, a separator, the "More categories" header, then Category 3–4.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
…ructor

Replace XamlReader.Load in MenuItemTemplateSelector with DataTemplate(DataTemplateElementFactory) from Windows App SDK 2.5.4-experimental, exercise the header/separator templates in the NavigationView data binding sample, and add a 'Create a DataTemplate in code' example to the Templates page.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@niels9001
Niels Laute (niels9001) merged commit c4021a5 into main Oct 7, 2026
4 checks passed
@niels9001
Niels Laute (niels9001) deleted the niels9001-winappsdk-experimental-code-apis branch October 7, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants