Created
November 20, 2015 15:59
-
-
Save asimmon/2c7c70ba94505c8231e9 to your computer and use it in GitHub Desktop.
EventToCommand Xamarin Forms - BindableBehavior Source
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
public class BindableBehavior<T> : Behavior<T> where T : BindableObject | |
{ | |
public T AssociatedObject { get; private set; } | |
protected override void OnAttachedTo(T visualElement) | |
{ | |
base.OnAttachedTo(visualElement); | |
AssociatedObject = visualElement; | |
if (visualElement.BindingContext != null) | |
BindingContext = visualElement.BindingContext; | |
visualElement.BindingContextChanged += OnBindingContextChanged; | |
} | |
private void OnBindingContextChanged(object sender, EventArgs e) | |
{ | |
OnBindingContextChanged(); | |
} | |
protected override void OnDetachingFrom(T view) | |
{ | |
view.BindingContextChanged -= 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