Created
March 7, 2018 17:26
-
-
Save FraukeN/2975169783cf388cf7fc6e6071b5cb85 to your computer and use it in GitHub Desktop.
BehaviorBase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Forms; | |
namespace EventToCommandBehavior | |
{ | |
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