Files
GreadyPoang/GreadyPoang.Common/Base/BehaviorBase.cs
2025-10-12 08:39:06 +02:00

31 lines
851 B
C#

namespace GreadyPoang.Common;
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
public T? AssociatedObject { get; private set; }
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null)
BindingContext = bindable.BindingContext;
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
void OnBindingContextChanged(object? sender, EventArgs e)
{
if (AssociatedObject != null)
BindingContext = AssociatedObject.BindingContext;
}
}