4 changed files with 104 additions and 0 deletions
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Avalonia.Data.Core |
|||
{ |
|||
public class TypeCastNode : ExpressionNode |
|||
{ |
|||
public override string Description => $"as {TargetType.FullName}"; |
|||
|
|||
public Type TargetType { get; } |
|||
|
|||
public TypeCastNode(Type type) |
|||
{ |
|||
TargetType = type; |
|||
} |
|||
|
|||
protected virtual object Cast(object value) |
|||
{ |
|||
return TargetType.IsInstanceOfType(value) ? value : null; |
|||
} |
|||
|
|||
protected override void StartListeningCore(WeakReference<object> reference) |
|||
{ |
|||
if (reference.TryGetTarget(out object target)) |
|||
{ |
|||
target = Cast(target); |
|||
reference = target == null ? NullReference : new WeakReference<object>(target); |
|||
} |
|||
|
|||
base.StartListeningCore(reference); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using Avalonia.Data.Core; |
|||
|
|||
namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings |
|||
{ |
|||
public class StrongTypeCastNode : TypeCastNode |
|||
{ |
|||
private Func<object, object> _cast; |
|||
|
|||
public StrongTypeCastNode(Type type, Func<object, object> cast) : base(type) |
|||
{ |
|||
_cast = cast; |
|||
} |
|||
|
|||
protected override object Cast(object value) |
|||
=> _cast(value); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue