From 733028e67bba3c1b9d720dfe5b4c97878e76ee36 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Sun, 14 Apr 2024 20:04:09 -0300 Subject: [PATCH 1/7] remove the IsSet check --- .../Behaviors/ICommunityToolkitBehavior.shared.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs index 0df155889..f4aabbe5f 100644 --- a/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs @@ -21,7 +21,7 @@ internal bool TrySetBindingContextToAttachedViewBindingContext() throw new InvalidOperationException($"{nameof(ICommunityToolkitBehavior)} can only be used for a {nameof(Behavior)}"); } - if (behavior.IsSet(BindableObject.BindingContextProperty) || View is null) + if (View is null) { return false; } @@ -33,7 +33,6 @@ internal bool TrySetBindingContextToAttachedViewBindingContext() }); return true; - } internal bool TryRemoveBindingContext() From 54165509fd39615f51a5e28c80bda49a93715fb3 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Sun, 14 Apr 2024 22:01:54 -0300 Subject: [PATCH 2/7] add verification to not set when the BindingContext is the same --- .../Behaviors/ICommunityToolkitBehavior.shared.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs index f4aabbe5f..d0806d19f 100644 --- a/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/ICommunityToolkitBehavior.shared.cs @@ -26,6 +26,11 @@ internal bool TrySetBindingContextToAttachedViewBindingContext() return false; } + if (View.BindingContext == behavior.BindingContext) + { + return false; + } + behavior.SetBinding(BindableObject.BindingContextProperty, new Binding { Source = View, From 6a0ba429009e9b448855c47d2575d41e197f0c9d Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Wed, 17 Apr 2024 20:04:00 +0100 Subject: [PATCH 3/7] something --- .../Behaviors/BaseBehavior.shared.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/CommunityToolkit.Maui/Behaviors/BaseBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/BaseBehavior.shared.cs index 7fc654e09..84840b349 100644 --- a/src/CommunityToolkit.Maui/Behaviors/BaseBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/BaseBehavior.shared.cs @@ -41,6 +41,26 @@ protected override void OnAttachedTo(TView bindable) base.OnAttachedTo(bindable); ((ICommunityToolkitBehavior)this).AssignViewAndBingingContext(bindable); + + bindable.PropertyChanging += (sender, args) => + { + var currentViewBindingContext = ((TView)sender).BindingContext; + + if (args.PropertyName == nameof(BindingContext) && + ReferenceEquals(this.BindingContext, currentViewBindingContext)) + { + this.BindingContext = null; + } + }; + + bindable.PropertyChanged += (sender, args) => + { + if (args.PropertyName == nameof(BindingContext) && + this.BindingContext is null) + { + this.BindingContext = ((TView?)sender)?.BindingContext; + } + }; } /// From f59357e1ac7115ea90a40c8b5d7422ea01082667 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:08:13 +0100 Subject: [PATCH 4/7] Revert IsSet check and add example to expose the original issue to the sample application --- .../Pages/Behaviors/TouchBehaviorPage.xaml | 43 ++++++++++++++++++- .../Pages/Behaviors/TouchBehaviorPage.xaml.cs | 10 +++++ .../ViewModels/Behaviors/ItemViewModel.cs | 19 ++++++++ .../Behaviors/TouchBehaviorViewModel.cs | 14 +++++- .../Behaviors/BaseBehavior.shared.cs | 20 --------- .../ICommunityToolkitBehavior.shared.cs | 7 +-- 6 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/ItemViewModel.cs diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/TouchBehaviorPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/TouchBehaviorPage.xaml index dfb0e713c..80493fea4 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/TouchBehaviorPage.xaml +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/TouchBehaviorPage.xaml @@ -37,7 +37,6 @@ PressedImageSource="button_pressed.png" /> -