Skip to content

Commit

Permalink
Fix BindingNotification handling in MultiBinding (#16102)
Browse files Browse the repository at this point in the history
* Added failing test for #16070.

* Handle BindingNotifications in UntypedObservableBindingExpression.

Fixes #16070
  • Loading branch information
grokys committed Jun 28, 2024
1 parent bbf11be commit 17cc674
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,18 @@ protected override void StopCore()

void IObserver<object?>.OnCompleted() { }
void IObserver<object?>.OnError(Exception error) { }
void IObserver<object?>.OnNext(object? value) => PublishValue(value);

void IObserver<object?>.OnNext(object? value)
{
if (value is BindingNotification n)
{
var v = n.Value;
var e = n.Error is not null ? new BindingError(n.Error, n.ErrorType) : null;
PublishValue(v, e);
}
else
{
PublishValue(value);
}
}
}
33 changes: 33 additions & 0 deletions tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ public void MultiBinding_Without_StringFormat_And_Converter()
Assert.Equal(target.ItemsView[2], source.C);
}

[Fact]
public void Converter_Can_Return_BindingNotification()
{
var source = new { A = 1, B = 2, C = 3 };
var target = new TextBlock { DataContext = source };

var binding = new MultiBinding
{
Converter = new BindingNotificationConverter(),
Bindings = new[]
{
new Binding { Path = "A" },
new Binding { Path = "B" },
new Binding { Path = "C" },
},
};

target.Bind(TextBlock.TextProperty, binding);

Assert.Equal("1,2,3-BindingNotification", target.Text);
}

private class ConcatConverter : IMultiValueConverter
{
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
Expand All @@ -203,5 +225,16 @@ public object Convert(IList<object> values, Type targetType, object parameter, C
return null;
}
}

private class BindingNotificationConverter : IMultiValueConverter
{
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
{
return new BindingNotification(
new ArgumentException(),
BindingErrorType.Error,
string.Join(",", values) + "-BindingNotification");
}
}
}
}

0 comments on commit 17cc674

Please sign in to comment.