Created
June 30, 2018 19:08
-
-
Save wsrzx/c16358471576751a8f4ca41d1b8f92dd 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 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