Skip to content

Commit

Permalink
Fix CompiledBinding with RelativeSource/ElementName but no Path (#14514)
Browse files Browse the repository at this point in the history
* Added failing tests for #14456.

And one passing test.

* Handle converted compiled binding nodes...

...without a path. Previously the `convertedNode` was being discarded if the binding node had no arguments or property value assignments.

Fixes #14456
  • Loading branch information
grokys authored and maxkatz6 committed Feb 8, 2024
1 parent 873bb90 commit b868322
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BindingExpressionGrammar.INode> { convertedNode };
binding.Arguments.Add(new ParsedBindingPathNode(binding, context.GetAvaloniaTypes().CompiledBindingPath, nodes));
}
}

return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,28 @@ public void ResolvesElementNameBindingFromLongForm()
}
}

[Fact]
public void ResolvesElementNameBindingFromLongFormWithoutPath()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
x:DataType='local:TestDataContext'>
<StackPanel>
<TextBlock Text='{CompiledBinding StringProperty}' x:Name='text' />
<TextBlock Text='{CompiledBinding ElementName=text}' x:Name='text2' />
</StackPanel>
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var textBlock = window.GetControl<TextBlock>("text2");

Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text);
}
}

[Fact]
public void ResolvesRelativeSourceBindingLongForm()
{
Expand Down Expand Up @@ -1493,6 +1515,28 @@ public void SupportCastToTypeInExpressionWithProperty_ExplicitPropertyCast()
}
}

[Fact]
public void Binds_To_Self()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
x:DataType='local:TestDataContext'>
<TextBlock Name='textBlock' Text='{CompiledBinding $self}' />
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var textBlock = window.GetControl<TextBlock>("textBlock");

window.ApplyTemplate();
window.Presenter!.ApplyTemplate();

Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text);
}
}

[Fact]
public void Binds_To_Self_Without_DataType()
{
Expand Down Expand Up @@ -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 = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
x:DataType='local:TestDataContext'>
<TextBlock Name='textBlock' Text='{CompiledBinding RelativeSource={RelativeSource Self}}' />
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var textBlock = window.GetControl<TextBlock>("textBlock");

window.ApplyTemplate();
window.Presenter!.ApplyTemplate();

Assert.Equal("Avalonia.Controls.TextBlock", textBlock.Text);
}
}

[Fact]
public void SupportsMethodBindingAsDelegate()
{
Expand Down

0 comments on commit b868322

Please sign in to comment.