From 92638a9bddbb0adcd9641469d4ac76fa112ee116 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 5 Jan 2024 12:50:39 +0100 Subject: [PATCH] Add IBinding2 support to observable bind overloads. In the case of `TemplateBinding`, the `IObservable` bind overload is selected by C#. Add an explicit check for an `IBinding2` here to use the more performant code-path. --- src/Avalonia.Base/AvaloniaObject.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs index 66765ede99..f5d2a6b9c8 100644 --- a/src/Avalonia.Base/AvaloniaObject.cs +++ b/src/Avalonia.Base/AvaloniaObject.cs @@ -455,7 +455,16 @@ namespace Avalonia VerifyAccess(); ValidatePriority(priority); - return _values.AddBinding(property, source, priority); + if (source is IBinding2 b) + { + if (b.Instance(this, property, null) is not UntypedBindingExpressionBase expression) + throw new NotSupportedException("Binding returned unsupported IBindingExpression."); + return GetValueStore().AddBinding(property, expression); + } + else + { + return _values.AddBinding(property, source, priority); + } } /// @@ -527,7 +536,16 @@ namespace Avalonia throw new ArgumentException($"The property {property.Name} is readonly."); } - return _values.AddBinding(property, source); + if (source is IBinding2 b) + { + if (b.Instance(this, property, null) is not UntypedBindingExpressionBase expression) + throw new NotSupportedException("Binding returned unsupported IBindingExpression."); + return GetValueStore().AddBinding(property, expression); + } + else + { + return _values.AddBinding(property, source); + } } ///