microsoft / microsoft/microsoft-ui-xaml

Binding with ElementName doesn't work from within a TreeViewItem

Open
#10,473 7 comments 3 reactions 0 assignees View on GitHub
area-Binding bug
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

When using a `Binding` with `ElementName` from within a `TreeViewItem` in a `DataTemplate`, the binding is not resolved and no binding errors are reported.

### Steps to reproduce the bug

```xaml












```

```csharp
using Microsoft.UI.Xaml;
using System.Collections.Generic;

namespace App10;

public sealed partial class MainWindow : Window
{
public MainWindow() =>
InitializeComponent();

public List ViewModels { get; } =
[
new("Parent 1")
{
Children=
[
new("Child 1.1"),
new("Child 1.2"),
new("Child 1.3"),
]
},
new("Parent 2")
{
Children=
[
new("Child 2.1"),
new("Child 2.2"),
new("Child 2.3"),
]
},
];

public string Annotation => "Annotation";
}

public class Entity(string name)
{
public string Name { get; } = name;

public List Children { get; set; } = [];
}
```

### Actual behavior

![Image](https://github.com/user-attachments/assets/8cf28e2d-2985-4c27-b76e-5bdbe142156b)

### Expected behavior

`Annotation` property should be accessed and materialized.

![Image](https://github.com/user-attachments/assets/73d81a41-a450-4b5e-9370-51e14c6c4664)

### Screenshots

_No response_

### NuGet package version

WinUI 3 - Windows App SDK 1.7.0: 1.7.250310001

### Windows version

Windows 11 (22H2): Build 22621

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the sample from MainWindow, focusing on the TreeViewItem, DataTemplate, and ElementName binding. Trace how the binding resolves the window reference, then verify that the Annotation value is displayed without binding errors when the fix is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.