Browse Source
* Added failing tests for #15081. * Provide target property in BindingExpression ctor. Usually it is not necessary to provide the target property when creating a `BindingExpression` because the property will be assigned when the binding expression is attached to the target in `BindingExpressionBase.Attach`. This is however one case where `Attach` is not called: when the obsolete `binding.Initiate` method is called and then an observable is read from the `InstancedBinding` without the binding actually being attached to the target object. In this case, prior to the binding refactor in #13970 the value produced by the observable was still converted to the target type. After #13970, because the target property (and hence the target type) is not yet set, the conversion is to the target type is no longer done. `DataGrid` uses this obsolete method when editing cells, causing #15081. Ideally we'd fix that in `DataGrid` but I'm not happy making this change so close to 11.1, so instead fix this use-case to behave as before. Fixes #15081pull/15722/head
committed by
GitHub
6 changed files with 65 additions and 4 deletions
@ -0,0 +1,51 @@ |
|||
using System.Reactive.Linq; |
|||
using Avalonia.Data; |
|||
using Avalonia.Markup.Xaml.MarkupExtensions; |
|||
using Xunit; |
|||
|
|||
#nullable enable |
|||
#pragma warning disable CS0618 // Type or member is obsolete
|
|||
|
|||
namespace Avalonia.Base.UnitTests.Data.Core; |
|||
|
|||
public abstract partial class BindingExpressionTests |
|||
{ |
|||
public partial class Reflection |
|||
{ |
|||
[Fact] |
|||
public void Obsolete_Initiate_Method_Produces_Observable_With_Correct_Target_Type() |
|||
{ |
|||
// Issue #15081
|
|||
var viewModel = new ViewModel { DoubleValue = 42.5 }; |
|||
var target = new TargetClass { DataContext = viewModel }; |
|||
var binding = new Binding(nameof(viewModel.DoubleValue)); |
|||
var instanced = binding.Initiate(target, TargetClass.StringProperty); |
|||
|
|||
Assert.NotNull(instanced); |
|||
|
|||
var value = instanced.Observable.First(); |
|||
|
|||
Assert.Equal("42.5", value); |
|||
} |
|||
} |
|||
|
|||
public partial class Compiled |
|||
{ |
|||
[Fact] |
|||
public void Obsolete_Initiate_Method_Produces_Observable_With_Correct_Target_Type() |
|||
{ |
|||
// Issue #15081
|
|||
var viewModel = new ViewModel { DoubleValue = 42.5 }; |
|||
var target = new TargetClass { DataContext = viewModel }; |
|||
var path = CompiledBindingPathFromExpressionBuilder.Build<ViewModel, double>(x => x.DoubleValue, true); |
|||
var binding = new CompiledBindingExtension(path); |
|||
var instanced = binding.Initiate(target, TargetClass.StringProperty); |
|||
|
|||
Assert.NotNull(instanced); |
|||
|
|||
var value = instanced.Observable.First(); |
|||
|
|||
Assert.Equal("42.5", value); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue