ARTICLE AD BOX
In my WinUI 3 application (using WindowsAppSDK 1.8.251106002), I try to use a data-bound ContentPresenter for displaying data from my view models in different ways depending on their data type. I want to use a custom ContentTemplateSelector for that purpose, as described in this Microsoft article. Unfortunately, this does not work as the SelectTemplateCore function always receives null for the item parameter.
Am I doing something wrong? Here is a simplified version of what I have attempted:
Generated output:

MainWindow.xaml:
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="WinUIApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinUIApp1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="WinUIApp1"> <Grid> <Grid.Resources> <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"/> </Grid.Resources> <StackPanel Orientation="Vertical" Spacing="10"> <!-- This does not load the data template, and displays text red in black: --> <ContentPresenter Content="{x:Bind _viewModel.Text, Mode=OneWay}" ContentTemplateSelector="{StaticResource MyDataTemplateSelector}"/> <!-- This loads the data template, and displays text red in red: --> <ContentPresenter Content="This is a test" ContentTemplateSelector="{StaticResource MyDataTemplateSelector}"/> </StackPanel> </Grid> </Window>MainWindow.xaml.cs:
using Microsoft.UI.Xaml; using System.ComponentModel; namespace WinUIApp1 { public partial class MyViewModel : INotifyPropertyChanged { private string _text = "This is a test"; public string Text { get => _text; set { if (_text != value) { _text = value; OnPropertyChanged(nameof(Text)); } } } protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler? PropertyChanged; } public sealed partial class MainWindow : Window { readonly MyViewModel _viewModel = new(); public MainWindow() { InitializeComponent(); } } }MyDataTemplateSelector.cs:
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace WinUIApp1; public partial class MyDataTemplateSelector : DataTemplateSelector { protected override DataTemplate SelectTemplateCore(object item) { if (item is string) { return (DataTemplate)Application.Current.Resources["MyDataTemplate"]; } return base.SelectTemplateCore(item); } protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { if (item is string) { return (DataTemplate)Application.Current.Resources["MyDataTemplate"]; } return base.SelectTemplateCore(item, container); } }App.xaml:
<?xml version="1.0" encoding="utf-8"?> <Application x:Class="WinUIApp1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:WinUIApp1"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> <!-- Other merged dictionaries here --> </ResourceDictionary.MergedDictionaries> <!-- Other app resources here --> <DataTemplate x:Key="MyDataTemplate"> <TextBlock Text="{Binding}" Foreground="Red" /> </DataTemplate> </ResourceDictionary> </Application.Resources> </Application>