Browse Source
Merge pull request #5198 from AvaloniaUI/fixes/nre-with-indexer
Fix: NullReferenceException when using TwoWay binding with indexer to null DataContext
pull/5299/head
Max Katz
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
27 additions and
3 deletions
-
src/Avalonia.Base/Data/Core/SettableNode.cs
-
src/Markup/Avalonia.Markup/Markup/Parsers/Nodes/StringIndexerNode.cs
-
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs
|
|
|
@ -15,7 +15,8 @@ namespace Avalonia.Data.Core |
|
|
|
|
|
|
|
private bool ShouldNotSet(object value) |
|
|
|
{ |
|
|
|
if (PropertyType == null) |
|
|
|
var propertyType = PropertyType; |
|
|
|
if (propertyType == null) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
@ -37,7 +38,7 @@ namespace Avalonia.Data.Core |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (PropertyType.IsValueType) |
|
|
|
if (propertyType.IsValueType) |
|
|
|
{ |
|
|
|
return lastValue.Equals(value); |
|
|
|
} |
|
|
|
|
|
|
|
@ -129,7 +129,10 @@ namespace Avalonia.Markup.Parsers.Nodes |
|
|
|
{ |
|
|
|
get |
|
|
|
{ |
|
|
|
Target.TryGetTarget(out object target); |
|
|
|
if (!Target.TryGetTarget(out object target)) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
return GetIndexer(target.GetType().GetTypeInfo())?.PropertyType; |
|
|
|
} |
|
|
|
|
|
|
|
@ -4,6 +4,8 @@ using System.Reactive.Linq; |
|
|
|
using System.Reactive.Subjects; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
using Avalonia.Controls; |
|
|
|
using Avalonia.Data; |
|
|
|
using Avalonia.Logging; |
|
|
|
using Avalonia.Platform; |
|
|
|
@ -797,6 +799,24 @@ namespace Avalonia.Base.UnitTests |
|
|
|
Assert.False(source.SetterCalled); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void TwoWay_Binding_Should_Not_Fail_With_Null_DataContext() |
|
|
|
{ |
|
|
|
var target = new TextBlock(); |
|
|
|
target.DataContext = null; |
|
|
|
|
|
|
|
target.Bind(TextBlock.TextProperty, new Binding("Missing", BindingMode.TwoWay)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void TwoWay_Binding_Should_Not_Fail_With_Null_DataContext_Indexer() |
|
|
|
{ |
|
|
|
var target = new TextBlock(); |
|
|
|
target.DataContext = null; |
|
|
|
|
|
|
|
target.Bind(TextBlock.TextProperty, new Binding("[0]", BindingMode.TwoWay)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Disposing_Completed_Binding_Does_Not_Throw() |
|
|
|
{ |
|
|
|
|