Skip to content

Instantly share code, notes, and snippets.

@wsrzx
Created June 30, 2018 19:08
Show Gist options
  • Save wsrzx/c16358471576751a8f4ca41d1b8f92dd to your computer and use it in GitHub Desktop.
Save wsrzx/c16358471576751a8f4ca41d1b8f92dd to your computer and use it in GitHub Desktop.
public static class AttachedItemTappedBehavior
{
public static readonly BindableProperty CommandProperty =
BindableProperty.CreateAttached(
propertyName: "Command",
returnType: typeof(ICommand),
declaringType: typeof(ListView),
defaultValue: null,
defaultBindingMode: BindingMode.OneWay,
validateValue: null,
propertyChanged: OnItemTappedChanged);
private static void OnItemTappedChanged(BindableObject bindable, object oldValue, object newValue)
=> (bindable as ListView).ItemTapped += (sender, e) =>
{
var control = sender as ListView;
var command = (ICommand)control.GetValue(CommandProperty);
if (command != null && command.CanExecute(e.Item))
command.Execute(e.Item);
control.SelectedItem = null;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment