Skip to content

Instantly share code, notes, and snippets.

@julesx
Created April 10, 2017 16:23
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 julesx/f69c8afa64f593733db80c2f8424dc22 to your computer and use it in GitHub Desktop.
Save julesx/f69c8afa64f593733db80c2f8424dc22 to your computer and use it in GitHub Desktop.
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