Browse Source
Merge pull request #7248 from jkoritzinsky/compiledbinding-datatype-on-binding
Add a DataType property on CompiledBindingExtension to enable specifying the start type explicitly on a binding.
pull/7251/head
Max Katz
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
16 additions and
1 deletions
-
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlBindingPathTransformer.cs
-
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlDataContextTypeTransformer.cs
-
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindingExtension.cs
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using XamlX.Ast; |
|
|
|
using XamlX.Transform; |
|
|
|
using XamlX.Transform.Transformers; |
|
|
|
using XamlX.TypeSystem; |
|
|
|
|
|
|
|
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
@ -15,7 +16,8 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
{ |
|
|
|
IXamlType startType = null; |
|
|
|
var sourceProperty = binding.Children.OfType<XamlPropertyAssignmentNode>().FirstOrDefault(c => c.Property.Name == "Source"); |
|
|
|
if ((sourceProperty?.Values.Count ?? 0) == 1) |
|
|
|
var dataTypeProperty = binding.Children.OfType<XamlPropertyAssignmentNode>().FirstOrDefault(c => c.Property.Name == "DataType"); |
|
|
|
if (sourceProperty?.Values.Count is 1) |
|
|
|
{ |
|
|
|
var sourceValue = sourceProperty.Values[0]; |
|
|
|
switch (sourceValue) |
|
|
|
@ -99,6 +101,11 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (dataTypeProperty?.Values.Count is 1 && dataTypeProperty.Values[0] is XamlAstTextNode text) |
|
|
|
{ |
|
|
|
startType = TypeReferenceResolver.ResolveType(context, text.Text, isMarkupExtension: false, text, strict: true).Type; |
|
|
|
} |
|
|
|
|
|
|
|
Func<IXamlType> startTypeResolver = startType is not null ? () => startType : () => |
|
|
|
{ |
|
|
|
var parentDataContextNode = context.ParentNodes().OfType<AvaloniaXamlIlDataContextTypeMetadataNode>().FirstOrDefault(); |
|
|
|
|
|
|
|
@ -155,6 +155,12 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers |
|
|
|
{ |
|
|
|
Func<IXamlType> startTypeResolver = () => |
|
|
|
{ |
|
|
|
var dataTypeProperty = obj.Children.OfType<XamlPropertyAssignmentNode>().FirstOrDefault(c => c.Property.Name == "DataType"); |
|
|
|
if (dataTypeProperty?.Values.Count is 1 && dataTypeProperty.Values[0] is XamlAstTextNode text) |
|
|
|
{ |
|
|
|
return TypeReferenceResolver.ResolveType(context, text.Text, isMarkupExtension: false, text, strict: true).Type; |
|
|
|
} |
|
|
|
|
|
|
|
var parentDataContextNode = context.ParentNodes().OfType<AvaloniaXamlIlDataContextTypeMetadataNode>().FirstOrDefault(); |
|
|
|
if (parentDataContextNode is null) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -71,5 +71,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions |
|
|
|
public CompiledBindingPath Path { get; set; } |
|
|
|
|
|
|
|
public object Source { get; set; } |
|
|
|
|
|
|
|
public Type DataType { get; set; } |
|
|
|
} |
|
|
|
} |
|
|
|
|