diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathParser.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathParser.cs index 736e764aa7c..8173f124d3b 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathParser.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathParser.cs @@ -66,8 +66,16 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod bindingPathAssignment.Values[0] = new ParsedBindingPathNode(pathValue, context.GetAvaloniaTypes().CompiledBindingPath, nodes); } + + foundPath = true; } } + + if (!foundPath && convertedNode != null) + { + var nodes = new List { convertedNode }; + binding.Arguments.Add(new ParsedBindingPathNode(binding, context.GetAvaloniaTypes().CompiledBindingPath, nodes)); + } } return node; diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs index 9d82674fb61..be8b3950ee7 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs @@ -824,6 +824,28 @@ public void ResolvesElementNameBindingFromLongForm() } } + [Fact] + public void ResolvesElementNameBindingFromLongFormWithoutPath() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = window.GetControl("text2"); + + Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text); + } + } + [Fact] public void ResolvesRelativeSourceBindingLongForm() { @@ -1493,6 +1515,28 @@ public void SupportCastToTypeInExpressionWithProperty_ExplicitPropertyCast() } } + [Fact] + public void Binds_To_Self() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = window.GetControl("textBlock"); + + window.ApplyTemplate(); + window.Presenter!.ApplyTemplate(); + + Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text); + } + } + [Fact] public void Binds_To_Self_Without_DataType() { @@ -1545,6 +1589,28 @@ public void Binds_To_Self_In_Style() } } + [Fact] + public void Binds_To_RelativeSource_Self() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = window.GetControl("textBlock"); + + window.ApplyTemplate(); + window.Presenter!.ApplyTemplate(); + + Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text); + } + } + [Fact] public void SupportsMethodBindingAsDelegate() {