WinUI3 TabView Binding Not Updating Correctly - Recycling Instances?

Eric Brandt 45 Reputation points
2025-01-13T17:40:45.3233333+00:00

Is this a bug in TabView implementation? Or what could I be doing wrong?

Summary:

Binding seems to get out of sync when adding and removing tabs from tab TabView.

Minimal Working Example that Demonstrates the Issue:

Code for minimal working example that demonstrates the issue here:

https://github.com/elbrandt/WinUI3_TabView_Binding_Issue

Description:

In a desktop WinUI3 app, using .NET8, I have a TabView that has its TabItemsSource property bound to an ObservableCollection of TabViewModel instances in the page's ViewModel class.

Each time a new tab button is clicked, a ViewModel command method is executed, which instantiates a TabViewModel class and adds it to the ObservableCollection. The TabViewModel class has a single integer property, MyNumber that is initialized to a unique, monotonically increasing integer.

The MyNumber property is bound to both the TabViewItem's Header property, as well as to a TextBlock in a custom control that is the content of the TabViewItem

Behavior (Reproducing):

The Header and and TextBlock should always be in sync, since they are bound to the same member of the object that is the DataContext for the TabViewItem.

However, if you 1. Add two tabs, 2. Close the two tabs, 3. Add another tab, then you will see the two UI elements are no longer in sync.

What is going on? Am I doing something wrong? Or is this a bug in the TabView code?

Thanks in advance!

(I have cross posted this to https://stackoverflow.com/questions/79353040/winui3-tabview-binding-not-updating-correctly-recycling-instances as well, in hopes of getting an answer somewhere, quickly).

User's image

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
809 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,191 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 10,356 Reputation points Microsoft Vendor
    2025-01-14T02:23:40.4866667+00:00

    Hi,

    Welcome to Microsoft Q&A!

    I tested the code that you provided. I found that the issue lies in the TabView reusing items.

    Based on Andrew's suggestion on SO, you could try to use a DependencyProperty.

    Thank you

    Jeanine

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.