Skip to content

TemplateSelector on ContentControl always passed null #5655

@yoshiask

Description

@yoshiask

Describe the bug
I have a bunch of view models (using MVVM Toolkit, so they implement INotifyPropertyChanged) that I want to display in a ContentControl with a DataTemplateSelector. My template selector overrides both SelectTemplateCore(object item) and SelectTemplateCore(object item, DependencyObject container), but item is always null. Using the exact same binding but without the template selector works as expected.

I have been able to repro this in several slightly different scenarios in my actual project, but copying the relevant code into a fresh project doesn't have the same issue.

Steps to reproduce the bug

  1. Clone https://github.com/yoshiask/FluentStore
  2. Look over the template selectors in use (they're both pretty much the same): https://github.com/yoshiask/FluentStore/blob/master/FluentStore/Controls/PackageDisplayTemplateSelector.cs, https://github.com/yoshiask/FluentStore/blob/master/FluentStore/Controls/ImageBaseTemplateSelector.cs
  3. Take a look at uses of ContentControl with a DataTemplateSelector: https://github.com/yoshiask/FluentStore/blob/master/FluentStore/Views/PackageView.xaml#L31, https://github.com/yoshiask/FluentStore/blob/master/FluentStore/Views/PackageView.xaml#L54, https://github.com/yoshiask/FluentStore/blob/master/FluentStore/Views/PackageView.xaml#L180
  4. Build and deploy the UWP project.
  5. On the home page, click on the "Free" button on any of the carousel items.
  6. Scroll down and observe that the ContentControl appears empty.

Expected behavior
The template selectors to be passed the values they are bound to.

Screenshots
image

Version Info

NuGet package version:
Microsoft.UI.Xaml 2.6.1

Windows version Saw the problem?
Insider Build (22000.120) Yes
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context
Although my attempt at a minimum repro wasn't successful, here's the link to it:
https://github.com/yoshiask/Repros/tree/master/ContentControlTemplateSelector
I'm not really sure why it works here but not in the main project, but maybe it's still useful for pinning down the issue.

Also, here is a link to a discussion in the UWP Community Discord server, so you can see what I have tried so far:
https://discord.com/channels/372137812037730304/580484470877061120/873706002816585739

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-winui-3Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3)no-issue-activityteam-ControlsIssue for the Controls team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions