Skip to content

Commit 58297e1

Browse files
committed
fix(xLoad): fix unload-reload scenarios when using x:Bind with x:Load
1 parent f6b559f commit 58297e1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Uno.UI/UI/Xaml/ElementStub.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ private void MaterializeInner()
203203
TemplatedParentScope.PushScope(GetTemplatedParent(), _fromLegacyTemplate);
204204
#endif
205205

206+
// Before swapping out the ElementStub, we set the inherited DC locally so that when removed from the
207+
// visual tree, the inherited DC is kept intact. This is important in case of setting x:Load with an
208+
// x:Bind. The binding we generate from the x:Bind will only listen to property changes while the DC
209+
// is present. This is definitely not what happens on WinUI, but considering our implementation of
210+
// x:Bind and ElementStub differ completely from WinUI's, this is acceptable for now.
211+
// https://github.com/unoplatform/uno/issues/18509
212+
if ((this as IDependencyObjectStoreProvider).Store.GetPropertyDetails(DataContextProperty)
213+
.CurrentHighestValuePrecedence > DependencyPropertyValuePrecedences.Local)
214+
{
215+
this.SetValue(DataContextProperty, DataContext, DependencyPropertyValuePrecedences.Local);
216+
}
217+
206218
_content = SwapViews(oldView: (FrameworkElement)this, newViewProvider: ContentBuilder);
207219

208220
MaterializationChanged?.Invoke(this);
@@ -226,6 +238,7 @@ private void Dematerialize()
226238

227239
if (_content != null)
228240
{
241+
this.ClearValue(DataContextProperty, DependencyPropertyValuePrecedences.Local);
229242
var newView = SwapViews(oldView: (FrameworkElement)_content, newViewProvider: () => this as View);
230243
if (newView != null)
231244
{

0 commit comments

Comments
 (0)