Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Fix MultiValidationBehavior overwrites external BindingContext bindings #1163

Merged
merged 3 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ static readonly FieldInfo? bindingField

protected TView? View { get; private set; }

internal bool TrySetBindingContext(Binding binding)
{
if (!IsBound(BindingContextProperty))
{
SetBinding(BindingContextProperty, defaultBindingContextBinding = binding);
return true;
}

return false;
}

internal bool TryRemoveBindingContext()
{
if (defaultBindingContextBinding != null)
{
RemoveBinding(BindingContextProperty);
defaultBindingContextBinding = null;

return true;
}

return false;
}

protected virtual void OnViewPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
}
Expand All @@ -30,28 +54,17 @@ protected override void OnAttachedTo(TView bindable)
base.OnAttachedTo(bindable);
View = bindable;
bindable.PropertyChanged += OnViewPropertyChanged;

if (!IsBound(BindingContextProperty))
TrySetBindingContext(new Binding
{
defaultBindingContextBinding = new Binding
{
Path = BindingContextProperty.PropertyName,
Source = bindable
};
SetBinding(BindingContextProperty, defaultBindingContextBinding);
}
Path = BindingContextProperty.PropertyName,
Source = bindable
});
}

protected override void OnDetachingFrom(TView bindable)
{
base.OnDetachingFrom(bindable);

if (defaultBindingContextBinding != null)
{
RemoveBinding(BindingContextProperty);
defaultBindingContextBinding = null;
}

TryRemoveBindingContext();
bindable.PropertyChanged -= OnViewPropertyChanged;
View = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void OnChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArg
{
foreach (var child in e.NewItems.OfType<ValidationBehavior>())
{
child.SetBinding(BindingContextProperty, new Binding
child.TrySetBindingContext(new Binding
{
Path = BindingContextProperty.PropertyName,
Source = this
Expand All @@ -103,7 +103,7 @@ void OnChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArg
if (e.OldItems != null)
{
foreach (var child in e.OldItems.OfType<ValidationBehavior>())
child.RemoveBinding(BindingContextProperty);
child.TryRemoveBindingContext();
}
}
}
Expand Down