Skip to content

Commit

Permalink
feat: Allow freezing of TemplatedParent
Browse files Browse the repository at this point in the history
Workaround for unoplatform#15749
  • Loading branch information
MartinZikmund committed Mar 6, 2024
1 parent 34b5663 commit 6308a3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Uno.UI/UI/Xaml/DependencyObjectStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ private void Dispose(bool disposing)
/// </remarks>
public bool IsAutoPropertyInheritanceEnabled { get; set; } = true;

internal bool IsTemplatedParentFrozen { get; set; }

/// <summary>
/// Gets a unique identifier for the current DependencyObject.
/// </summary>
Expand Down Expand Up @@ -440,6 +442,11 @@ void SetValueWithTrace(DependencyProperty property, object? value, DependencyPro

private void InnerSetValue(DependencyProperty property, object? value, DependencyPropertyValuePrecedences precedence, DependencyPropertyDetails? propertyDetails, bool isPersistentResourceBinding)
{
if (IsTemplatedParentFrozen && property == FrameworkElement.TemplatedParentProperty)
{
return;
}

if (precedence == DependencyPropertyValuePrecedences.Coercion)
{
throw new ArgumentException("SetValue must not be called with precedence DependencyPropertyValuePrecedences.Coercion, as it expects a non-coerced value to function properly.");
Expand Down Expand Up @@ -1218,7 +1225,10 @@ private void CleanupInheritedProperties()


SetValue(_dataContextProperty!, DependencyProperty.UnsetValue, DependencyPropertyValuePrecedences.Inheritance);
SetValue(_templatedParentProperty!, DependencyProperty.UnsetValue, DependencyPropertyValuePrecedences.Inheritance);
if (!IsTemplatedParentFrozen)
{
SetValue(_templatedParentProperty!, DependencyProperty.UnsetValue, DependencyPropertyValuePrecedences.Inheritance);
}
}
}
finally
Expand Down Expand Up @@ -1563,8 +1573,10 @@ void Propagate(DependencyObjectStore store)
for (var propertyIndex = 0; propertyIndex < props.Length; propertyIndex++)
{
var prop = props[propertyIndex];

store.OnParentPropertyChangedCallback(instanceRef, prop, GetValue(prop));
if (!IsTemplatedParentFrozen || prop != TemplatedParentProperty)
{
store.OnParentPropertyChangedCallback(instanceRef, prop, GetValue(prop));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public partial class UIElement : DependencyObject, IXUidProvider, IUIElement
private InputCursor _protectedCursor;
private SerialDisposable _disposedEventDisposable = new();

internal void FreezeTemplatedParent() =>
((IDependencyObjectStoreProvider)this).Store.IsTemplatedParentFrozen = true;

//private protected virtual void PrepareState()
//{
// // This is part of the WinUI internal API and is invoked at the end of DXamlCore.GetPeerPrivate
Expand Down

0 comments on commit 6308a3c

Please sign in to comment.