Skip to content

Commit

Permalink
fix: Avoid unsetting item container DataContext
Browse files Browse the repository at this point in the history
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 02b42f4)
  • Loading branch information
MartinZikmund authored and mergify[bot] committed Jul 20, 2023
1 parent af493d0 commit 5250ebc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 5250ebc

Please sign in to comment.