AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.06k stars 2.25k forks source link

Binding on a UserControl inside a DataTemplate broken in 11.2.0 #17474

Open Asttear opened 2 weeks ago

Asttear commented 2 weeks ago

Describe the bug

DataContext={Binding} on a UserControl inside a DataTemplate is broken in 11.2.0

11.2.0 image

11.1.4 image

To Reproduce

MainWindow.axaml

...
    <ContentControl Content="{Binding Book}">
        <ContentControl.DataTemplates>
            <DataTemplate DataType="m:Book">
                <StackPanel>
                    <TextBlock Background="Red" Text="{Binding}" />

                    <!-- <v:UserControl1 Background="Yellow" /> will be OK -->
                    <!--  however the following will be invisible  -->
                    <v:UserControl1 Background="Yellow" DataContext="{Binding}" />

                    <ContentControl Background="Green" Content="{Binding}">
                        <ContentControl.DataTemplates>
                            <DataTemplate x:DataType="m:Book">
                                <StackPanel>
                                    <TextBlock Text="{Binding}" />
                                </StackPanel>
                            </DataTemplate>
                        </ContentControl.DataTemplates>
                    </ContentControl>
                </StackPanel>
            </DataTemplate>
        </ContentControl.DataTemplates>
    </ContentControl>
...

UserControl1.axaml

...
    <ContentControl Content="{Binding}">
        <ContentControl.DataTemplates>
            <DataTemplate x:DataType="m:Book">
                <StackPanel>
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </ContentControl.DataTemplates>
    </ContentControl>
...

MainWindowViewModel.cs

public partial class MainWindowViewModel : ViewModelBase
{
    public Book Book { get; } = new();
}

Book.cs

public record Book(string Name = "Avalonia", int Price = 4000);

Expected behavior

Binding works properly as in 11.1.4

Avalonia version

11.2.0

OS

Windows

Additional context

May be related to #16242