Browse Source

Fix ReactiveUI regression, when DataContext type is different (#15423)

release/11.1.0-beta2
Max Katz 2 years ago
parent
commit
05a742bc5e
  1. 5
      src/Avalonia.ReactiveUI/ReactiveUserControl.cs
  2. 5
      src/Avalonia.ReactiveUI/ReactiveWindow.cs
  3. 16
      tests/Avalonia.ReactiveUI.UnitTests/ReactiveUserControlTest.cs

5
src/Avalonia.ReactiveUI/ReactiveUserControl.cs

@ -48,14 +48,15 @@ namespace Avalonia.ReactiveUI
if (change.Property == DataContextProperty)
{
if (Object.ReferenceEquals(change.OldValue, ViewModel))
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or TViewModel)
{
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty)
{
if (Object.ReferenceEquals(change.OldValue, DataContext))
if (ReferenceEquals(change.OldValue, DataContext))
{
SetCurrentValue(DataContextProperty, change.NewValue);
}

5
src/Avalonia.ReactiveUI/ReactiveWindow.cs

@ -48,14 +48,15 @@ namespace Avalonia.ReactiveUI
if (change.Property == DataContextProperty)
{
if (Object.ReferenceEquals(change.OldValue, ViewModel))
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or TViewModel)
{
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty)
{
if (Object.ReferenceEquals(change.OldValue, DataContext))
if (ReferenceEquals(change.OldValue, DataContext))
{
SetCurrentValue(DataContextProperty, change.NewValue);
}

16
tests/Avalonia.ReactiveUI.UnitTests/ReactiveUserControlTest.cs

@ -134,6 +134,22 @@ namespace Avalonia.ReactiveUI.UnitTests
Assert.Same(vm2, view.ViewModel);
}
// https://github.com/AvaloniaUI/Avalonia/issues/15060
[Fact]
public void Should_Not_Inherit_DataContext_Of_Wrong_Type()
{
var view = new ExampleView();
var root = new TestRoot(view);
Assert.Null(view.DataContext);
Assert.Null(view.ViewModel);
root.DataContext = this;
Assert.Same(this, view.DataContext);
Assert.Null(view.ViewModel);
}
[Fact]
public void Should_Not_Overlap_Change_Notifications()
{

Loading…
Cancel
Save