-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes spurious DataGrid data validation error (#15716)
* 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 #15081
- Loading branch information
Showing
6 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/Avalonia.Base.UnitTests/Data/Core/BindingExpressionTests.Obsolete.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters