Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DataType property on CompiledBindingExtension to enable specifying the start type explicitly on a binding. #7248

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using XamlX.Ast;
using XamlX.Transform;
using XamlX.Transform.Transformers;
using XamlX.TypeSystem;

namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
Expand All @@ -15,7 +16,8 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
{
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)
Expand Down Expand Up @@ -99,6 +101,11 @@ propertyNode.Values[0] is XamlAstConstructableObjectNode obj &&
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ private static AvaloniaXamlIlDataContextTypeMetadataNode ParseDataContext(AstTra
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ protected override ExpressionObserver CreateExpressionObserver(IAvaloniaObject t
public CompiledBindingPath Path { get; set; }

public object Source { get; set; }

public Type DataType { get; set; }
}
}