Created
April 10, 2017 16:23
-
-
Save julesx/f69c8afa64f593733db80c2f8424dc22 to your computer and use it in GitHub Desktop.
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 VisualElementLostFocusBehavior : Behavior<VisualElement> | |
{ | |
public static readonly BindableProperty CommandProperty = | |
BindableProperty.Create("Command", typeof(ICommand), typeof(VisualElementLostFocusBehavior)); | |
public ICommand Command | |
{ | |
get { return (ICommand)GetValue(CommandProperty); } | |
set { SetValue(CommandProperty, value); } | |
} | |
private VisualElement _bindable; | |
protected override void OnAttachedTo(VisualElement bindable) | |
{ | |
base.OnAttachedTo(bindable); | |
_bindable = bindable; | |
bindable.BindingContextChanged += BindableOnBindingContextChanged; | |
bindable.Unfocused += BindableOnUnfocused; | |
} | |
private void BindableOnBindingContextChanged(object sender, EventArgs eventArgs) | |
{ | |
this.BindingContext = _bindable.BindingContext; | |
} | |
private void BindableOnUnfocused(object sender, FocusEventArgs focusEventArgs) | |
{ | |
Command?.Execute(null); | |
} | |
protected override void OnDetachingFrom(VisualElement bindable) | |
{ | |
base.OnDetachingFrom(bindable); | |
bindable.BindingContextChanged -= BindableOnBindingContextChanged; | |
bindable.Unfocused -= BindableOnUnfocused; | |
_bindable = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment