Skip to content

Instantly share code, notes, and snippets.

@adityadeshpande
Created January 16, 2018 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adityadeshpande/aee7075940102c6546a1b99a3eb6b2ca to your computer and use it in GitHub Desktop.
Save adityadeshpande/aee7075940102c6546a1b99a3eb6b2ca to your computer and use it in GitHub Desktop.
using System;
using Xamarin.Forms;
namespace MyNameSpace
{
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)
{
OnBindingContextChanged();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment