Browse Source
Merge pull request #7813 from MarchingCube/fix-compiled-self-binding
Fixed compiled self binding not working from setters
pull/7830/head
Max Katz
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
73 additions and
1 deletions
-
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathTransformer.cs
-
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs
-
tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs
-
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs
|
|
|
@ -117,7 +117,15 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
return parentDataContextNode.DataContextType; |
|
|
|
}; |
|
|
|
|
|
|
|
XamlIlBindingPathHelper.UpdateCompiledBindingExtension(context, binding, startTypeResolver, context.ParentNodes().OfType<XamlAstConstructableObjectNode>().First().Type.GetClrType()); |
|
|
|
var selfType = context.ParentNodes().OfType<XamlAstConstructableObjectNode>().First().Type.GetClrType(); |
|
|
|
|
|
|
|
// When using self bindings with setters we need to change target type to resolved selector type.
|
|
|
|
if (context.GetAvaloniaTypes().ISetter.IsAssignableFrom(selfType)) |
|
|
|
{ |
|
|
|
selfType = context.ParentNodes().OfType<AvaloniaXamlIlTargetTypeMetadataNode>().First().TargetType.GetClrType(); |
|
|
|
} |
|
|
|
|
|
|
|
XamlIlBindingPathHelper.UpdateCompiledBindingExtension(context, binding, startTypeResolver, selfType); |
|
|
|
} |
|
|
|
|
|
|
|
return node; |
|
|
|
|
|
|
|
@ -91,6 +91,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
public IXamlType TextDecorationCollection { get; } |
|
|
|
public IXamlType TextDecorations { get; } |
|
|
|
public IXamlType TextTrimming { get; } |
|
|
|
public IXamlType ISetter { get; } |
|
|
|
|
|
|
|
public AvaloniaXamlIlWellKnownTypes(TransformerConfiguration cfg) |
|
|
|
{ |
|
|
|
@ -199,6 +200,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
TextDecorationCollection = cfg.TypeSystem.GetType("Avalonia.Media.TextDecorationCollection"); |
|
|
|
TextDecorations = cfg.TypeSystem.GetType("Avalonia.Media.TextDecorations"); |
|
|
|
TextTrimming = cfg.TypeSystem.GetType("Avalonia.Media.TextTrimming"); |
|
|
|
ISetter = cfg.TypeSystem.GetType("Avalonia.Styling.ISetter"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -1064,6 +1064,37 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Binds_To_Self_In_Style() |
|
|
|
{ |
|
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow)) |
|
|
|
{ |
|
|
|
var xaml = @"
|
|
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
|
|
|
|
|
|
|
<Window.Styles> |
|
|
|
<Style Selector='Button'> |
|
|
|
<Setter Property='IsVisible' Value='{CompiledBinding $self.IsEnabled}' /> |
|
|
|
</Style> |
|
|
|
</Window.Styles> |
|
|
|
|
|
|
|
<Button Name='button' /> |
|
|
|
</Window>";
|
|
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); |
|
|
|
var button = window.FindControl<Button>("button"); |
|
|
|
|
|
|
|
window.ApplyTemplate(); |
|
|
|
window.Presenter.ApplyTemplate(); |
|
|
|
|
|
|
|
Assert.True(button.IsVisible); |
|
|
|
|
|
|
|
button.IsEnabled = false; |
|
|
|
|
|
|
|
Assert.False(button.IsVisible); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void SupportsMethodBindingAsDelegate() |
|
|
|
{ |
|
|
|
|
|
|
|
@ -183,6 +183,37 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Binding_To_Self_In_Style_Works() |
|
|
|
{ |
|
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow)) |
|
|
|
{ |
|
|
|
var xaml = @"
|
|
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
|
|
|
|
|
|
|
<Window.Styles> |
|
|
|
<Style Selector='Button'> |
|
|
|
<Setter Property='IsVisible' Value='{Binding $self.IsEnabled}' /> |
|
|
|
</Style> |
|
|
|
</Window.Styles> |
|
|
|
|
|
|
|
<Button Name='button' /> |
|
|
|
</Window>";
|
|
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); |
|
|
|
var button = window.FindControl<Button>("button"); |
|
|
|
|
|
|
|
window.ApplyTemplate(); |
|
|
|
window.Presenter.ApplyTemplate(); |
|
|
|
|
|
|
|
Assert.True(button.IsVisible); |
|
|
|
|
|
|
|
button.IsEnabled = false; |
|
|
|
|
|
|
|
Assert.False(button.IsVisible); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Stream_Binding_To_Observable_Works() |
|
|
|
{ |
|
|
|
|