Skip to content

TreeViewItem.IsExpanded two-way x:Bind binding does not work in TreeView.ItemTemplate #10999

@PetrAltman

Description

@PetrAltman

Describe the bug

TreeViewItem.IsExpanded with {x:Bind ..., Mode=TwoWay} does not work when used inside a TreeView.ItemTemplate. The binding from ViewModel → View is ignored — programmatically setting the bound property does not always expand/collapse the tree node.

The issue is likely related to virtualization — when items are recycled, the IsExpanded state from the ViewModel is not applied to the newly materialized TreeViewItem.

Why is this important?

I'm building a client application that displays file diffs in a hierarchical tree structure (folders → files → diff content). Users need to expand/collapse tree nodes both manually and programmatically (e.g. "Expand All" / "Collapse All" buttons).

Steps to reproduce the bug

  1. Create a TreeView with a hierarchical ItemTemplate
  2. Inside the DataTemplate, place a TreeViewItem with IsExpanded="{x:Bind IsExpanded, Mode=TwoWay}"
  3. The ViewModel property implements INotifyPropertyChanged
  4. Set IsExpanded = true on a ViewModel item programmatically
  5. Observe that the tree nodes do not expand
<TreeView ItemsSource="{x:Bind Items,Mode=OneWay}">
    <TreeView.ItemTemplate>
        <DataTemplate x:DataType="vm:TreeItemViewModel">
            <TreeViewItem ItemsSource="{x:Bind Items}" 
                          IsExpanded="{x:Bind IsExpanded, Mode=TwoWay}">
                <TextBlock Text="{x:Bind Name}"/>
            </TreeViewItem>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Actual behavior

The IsExpanded binding from ViewModel → View has sometimes no effect. The tree nodes do not expand/collapse reliable when the bound property changes.

Expected behavior

Setting IsExpanded = true on the ViewModel should expand the corresponding TreeViewItem, and collapsing a node in the UI should set the ViewModel property to false.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.8.3: 1.8.251106002

Windows version

Windows 11 (24H2): Build 26100

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions