Skip to content

x:Bind and DataTemplates updating two way bindings incorrectly for selector based controls #5015

@tkillingerAlro

Description

@tkillingerAlro

Wondering if the WindowsCommunityToolkit team could find a solution to this via the mvvm toolkit. Was originally posted on Microsoft's Windows App SDK repo but it has been 3 months and no update on the bug fix. Without this fix using selector based controls in DataTemplates do not work and this is an extremely common use case.

This affects WinUI as a whole but also the ListDetailsView control.

Original: microsoft/microsoft-ui-xaml#10232

When using x:Bind with DataTemplates collection based controls are resetting selected items to null. This has been happening since I started using the Windows App SDK around version 1.0 and is becoming increasingly hard to code around.

Ones I have noticed are Combobox and ListView (I'm sure there are others). This seems to be related to the Selector Base Class.

https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.primitives.selector?view=windows-app-sdk-1.6

I have tried to look into the generated code and it seems like the following is happening.

A DataTemplates base binding object is set to another object due to the reuse of cached DataTemplates.
Selector based sub template controls check if the selected item is in the base binding object's new collection.
It's not due to the change of the whole binding object changing.
Selected Item is set to null
I have attached a sample project to demonstrate this.
This issue exists in standard WinUI controls and the Windows Community Toolkit ListDetailsView

Steps to reproduce the bug

Open up example project
Go to "x:Bind Control INotifyPropertyChanged" page
Click on the different list items change
You should notice the sub lists "Selected ***: value" TextBlocks are changing to Null
Go to "x:Bind Data Template" page
Click on the different list items change.
You should notice the sub lists "Selected ***: value" TextBlocks are changing to Null
Expected behavior
When using x:Bind in a DataTemplate the selected items should not be set to null. DataTemplates may contain a multitude of selector based sub controls. Leads to a mess in the ViewModel when trying to determine if the user changed something on the screen.

Screenshots

Image

Image

Image

ItemSourceBindingIssues.zip

NuGet package version
None

Packaging type
No response

Windows version
No response

IDE
Visual Studio 2022

Additional context
No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions