microsoft / microsoft/microsoft-ui-xaml
The ContentControl displays the object type name instead of rendering the DataTemplate after the ContentTemplateSelector has been set [WINUI]
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
The ContentControl displays the object type name instead of rendering the DataTemplate after the ContentTemplateSelector has been set. The same implementation works as expected in WPF.
```
**MainWindow.xaml**
**MainWindow.xaml.cs
public sealed partial class MainWindow : Window
{
public Person MyPerson { get; set; } = new Person { Name = "John Doe" };
public MainWindow()
{
this.InitializeComponent();
stackPanel.DataContext = this;
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
AppState.IsEditing = true;
MyContentControl.ContentTemplate = null;
MyContentControl.ContentTemplateSelector = (DataTemplateSelector)App.Current.Resources["MyTemplateSelector"];
var model = MyPerson;
MyContentControl.Content = null;
MyContentControl.Content = model;
}
}
**App.xaml**
**Person.cs**
public class Person
{
public string Name { get; set; }
}
**AppState.cs**
public static class AppState
{
public static bool IsEditing = false;
}
**MySelector.cs**
public class MySelector : DataTemplateSelector
{
public DataTemplate ViewTemplate { get; set; }
public DataTemplate EditTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
return AppState.IsEditing ? EditTemplate : ViewTemplate;
}
}
```
### Steps to reproduce the bug
1. Run the sample
2. Click the "Enter Edit Mode" button.
**Observed Behavior**
The ContentControl displays the underlying object’s type name instead of rendering the expected DataTemplate provided by the ContentTemplateSelector.
### Expected behavior
When clicking the "Enter Edit Mode" button, the ContentControl should show the edit template instead of the object’s type name.
### Screenshots
### WinUI Demo Video ###
https://github.com/user-attachments/assets/6cc613ca-6e8d-4efa-aa1f-a44ba5d150a4
### WPF Demo Video ###
https://github.com/user-attachments/assets/8362de70-67c9-4da5-8102-3bb4bc03757b
### NuGet package version
WinUI 3 - Windows App SDK 1.7.2: 1.7.250513003
### Windows version
Windows 11 (24H2): Build 26100
### Additional context
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided MainWindow.xaml and MainWindow.xaml.cs sample with the App.xaml resources, Person.cs, AppState.cs, and MySelector.cs definitions on WinUI 3 / Windows App SDK 1.7.2. Reproduce the Enter Edit Mode action and trace ContentControl's template selection and content update behavior. Done means the edit DataTemplate renders instead of the Person type name after the selector is assigned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, csharp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100