Skip to content

WinUI 3 DataTemplateSelector with x:Bind (x:DataType) works in Debug but crashes in Release #10310

@FoxitJohnny

Description

@FoxitJohnny

Describe the bug

I have a WinUI 3 app with a DataTemplateSelector and typed DataTemplates that use x:Bind and x:DataType="MyNamespace.Setting". Everything works perfectly in Debug mode, but in Release mode the app crashes with a NullReferenceException (raised from external code, so I don’t get a clear stack trace).

Steps to reproduce the bug

  • I have a ListView bound to an ObservableCollection<Setting>.
  • Inside the ListView.ItemTemplate, I use a <ContentControl ContentTemplateSelector="{StaticResource MySelector}" Content="{x:Bind}" />.
  • My DataTemplateSelector overrides SelectTemplateCore(object item, DependencyObject container), returning one of two typed data templates:
<DataTemplate x:Key="StringTemplate" x:DataType="models:Setting">
    <StackPanel>
        <TextBlock Text="{x:Bind Name}" />
        <TextBox Text="{x:Bind Value, Mode=TwoWay}" />
    </StackPanel>
</DataTemplate>

<DataTemplate x:Key="NumberTemplate" x:DataType="models:Setting">
    <StackPanel>
        <TextBlock Text="{x:Bind Name}" />
        <TextBox Text="{x:Bind Value, Mode=TwoWay}" />
    </StackPanel>
</DataTemplate>
  • My model is:
public enum SettingDataType { String, Number }

public class Setting
{
    public string Name { get; set; }
    public object Value { get; set; }
    public SettingDataType DataType { get; set; }
}

Expected behavior

When using WinUI 3 with typed DataTemplates (x:DataType="models:Setting") and a DataTemplateSelector, the same code and XAML that work in Debug should also run successfully in Release. Specifically:

  • SelectTemplateCore is called for each item to choose the correct DataTemplate at runtime.
  • The typed DataTemplate (with x:DataType and x:Bind) should be compiled without errors or missing references in both Debug and Release configurations.
  • The app should not crash or throw a NullReferenceException when loading the templates in Release.

In other words, the linker and runtime should preserve all necessary types, properties, and references so the UI renders properly without any special build-time or runtime differences between Debug and Release.

Screenshots

Running in Debug:
Image

Running in Release:
Image

TestBinding2.zip

NuGet package version

None

Windows version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-author-feedbackAsked author to supply more information.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions