From 5250ebc8bba5c68c9c1215555344344e314ac832 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 12 Jul 2023 14:10:01 +0200 Subject: [PATCH] fix: Avoid unsetting item container DataContext Previously we were clearing ItemsControl items' DataContext instead of nulling them out. Unsetting the DP value caused inheritance to take over, and that meant the item temporarily gained the DataContext of the ItemsControl - which could have been a UIElement, which then caused it to become the item's Content. This caused very odd issues like #12845. (cherry picked from commit 02b42f4ab47647e37bea71f84c58087f1d838c22) --- src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs b/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs index 39982032b3a8..57c8dd66da56 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs @@ -1200,9 +1200,11 @@ static void ClearPropertyWhenNoExpression(ContentControl target, DependencyPrope } } - // We are clearing the DataContext last. Because if there is a binding set on any of the above properties, Content(Template(Selector)?)?, - // clearing the DC can cause the data-bound property to be unnecessarily re-evaluated with an inherited DC from the visual parent. - contentControl.ClearValue(DataContextProperty); + // We are changing the DataContext last. Because if there is a binding set on any of the above properties, Content(Template(Selector)?)?, + // changing the DC can cause the data-bound property to be unnecessarily re-evaluated with an inherited DC from the visual parent. + // We also need to set value to null explicitly, because just unsetting would cause the DataContext to be inherited from the visual parent, + // which then causes issues like #12845. + contentControl.SetValue(DataContextProperty, null); } }