Skip to content

Binding with ElementName doesn't work from within a TreeViewItem #10473

@weitzhandler

Description

@weitzhandler

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

<Window
    x:Class="App10.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App10"
    x:Name="window">

    <TreeView ItemsSource="{x:Bind ViewModels}">
        <TreeView.ItemTemplate>
            <DataTemplate x:DataType="local:Entity">
                <TreeViewItem ItemsSource="{x:Bind Children}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{x:Bind Name}" />
                        <TextBlock Text="{Binding StuffProcessor.StuffTitle, ElementName=window}" />
                    </StackPanel>
                </TreeViewItem>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Window>
using Microsoft.UI.Xaml;
using System.Collections.Generic;

namespace App10;

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

    public List<Entity> 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<Entity> Children { get; set; } = [];
}

Actual behavior

Image

Expected behavior

Annotation property should be accessed and materialized.

Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions