Skip to content

Instantly share code, notes, and snippets.

@dkudelko
Created January 26, 2016 06:22
Show Gist options
  • Save dkudelko/778ec86b17ff8117815f to your computer and use it in GitHub Desktop.
Save dkudelko/778ec86b17ff8117815f to your computer and use it in GitHub Desktop.
ItemList MVVM Tap Command
using System;
using System.Windows.Input;
using Xamarin.Forms;
namespace YourNS {
public class ListView : Xamarin.Forms.ListView {
public static BindableProperty ItemClickCommandProperty = BindableProperty.Create<ListView, ICommand>(x => x.ItemClickCommand, null);
public ListView() {
this.ItemTapped += this.OnItemTapped;
}
public ICommand ItemClickCommand {
get { return (ICommand)this.GetValue(ItemClickCommandProperty); }
set { this.SetValue(ItemClickCommandProperty, value); }
}
private void OnItemTapped(object sender, ItemTappedEventArgs e) {
if (e.Item != null && this.ItemClickCommand != null && this.ItemClickCommand.CanExecute(e)) {
this.ItemClickCommand.Execute(e.Item);
this.SelectedItem = null;
}
}
}
}
ContentPage ...
xmlns:local="clr-namespace:Samples.Views;assembly=Your Assebly Name">
<local:ListView ItemClickCommand="{Binding TapCommand}"
ItemsSource="{Binding List}">
private Command<Signature> _tapCommand;
public Command<Signature> TapCommand {
get {
this._tapCommand = this._tapCommand ?? new Command<Signature>(s =>
this.dialogs.ActionSheet(new ActionSheetConfig()
.Add("View", () => {
if (!this.fileViewer.Open(s.FilePath))
this.dialogs.Alert(String.Format("Could not open file {0}", s.FileName));
})
.Add("Cancel")
)
);
return this._tapCommand;
}
}
public ViewModelCase1()
{
ItemList = new List<SampleModel>().ToObservableCollection();
TapCommand = new Command((e) =>
{
var itemselected = e as SampleModel;
Debug.WriteLine(e);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment