Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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