Skip to content

Commit

Permalink
chore: Adding back ApplyDataContext for targets where HAS_UNO is false
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph committed Jun 20, 2024
1 parent 47dbddc commit b9abdbb
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView
parentAsContentControl = parentAsContentControl ?? (VisualTreeHelper.GetParent(oldView) as ContentPresenter)?.FindFirstParent<ContentControl>();
#endif

#if !HAS_UNO
var parentDataContext = (parentAsContentControl as FrameworkElement)?.DataContext;
var oldDataContext = oldView.DataContext;
#endif
if ((parentAsContentControl?.Content as FrameworkElement) == oldView)
{
parentAsContentControl.Content = newView;
Expand Down Expand Up @@ -108,6 +112,37 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView
VisualTreeHelper.SwapViews(oldView, newView);
}
#endif

#if !HAS_UNO
if (oldView is FrameworkElement oldViewAsFE && newView is FrameworkElement newViewAsFE)
{
ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE, oldDataContext);
}
#endif
}

#if !HAS_UNO
private static void ApplyDataContext(
object? parentDataContext,
FrameworkElement oldView,
FrameworkElement newView,
object? oldDataContext)
{
if (oldView == null || newView == null)
{
return;
}

if ((newView.DataContext is null || newView.DataContext == parentDataContext)
&& (oldDataContext is not null && oldDataContext != parentDataContext))
{
// If the DataContext is not provided by the page itself, it may
// have been provided by an external actor. Copy the value as is
// in the DataContext of the new element.

newView.DataContext = oldDataContext;
}
}
#endif
}
}

0 comments on commit b9abdbb

Please sign in to comment.