title: Xamarin Native, Binding actions to listview items
body: <p>I would like to ask about bindings. What is the best approach to bind some actions in listview items in ios and android using xamarin in mvvm world. As I understand, we have few approaches.</p>
  
  <p>1.
  For every list item we have some <code>Model</code>, and to this model we have to add some <code>Commands</code>.</p>
  
  <p>For example:</p>
  
  <pre><code>public class ItemModel
  {
      public string MyName { get; set; }
      public ICommand RemoveCommand { get; set; }
  }
  </code></pre>
  
  <p>Where in <code>ViewModel</code> we have <code>SomeInitMethod</code></p>
  
  <pre><code>public class ViewModel
  {
      public ObservableCollection&lt;ItemModel&gt; Items {get;set;}
  
      public async Task SomeInitMethod
      {
          Items = new ObservableCollection(await _myApiService.FetchItemsAsync());
  
          foreach(var item in Items)
          {
              item.Command = new RelayCommand(RemoveItem);
          }
      }
  
      public void RemoveItem(ItemModel item)
      {
          Items.Remove(item);
      }
  }
  </code></pre>
  
  <p>But I see a drawback in <code>SomeInitMethod</code> where we should set <code>RemoveCommand</code>. What if we should to set 2 or even more commands than we duplicate code in <code>ListItemView</code>(somehow we need to bind all these commands)? </p>
  
  <ol start="2">
  <li>Next approach is somehow handle events of remove/toggle buttons and others in <code>Listview</code> and then delegate this commands directly to <code>ViewModel</code>. </li>
  </ol>
  
  <p>Example:</p>
  
  <pre><code>ContactsListView.ItemRemoveClicked += (ItemModel model) =&gt; ViewModel.RemoveItem
  </code></pre>
  
  <p>Advantages is: we no longer need to handle commands in ViewModel
  Drawback is: we need every time to write custom ListView and support event handling in code-behind.</p>
  
  <ol start="3">
  <li>The last approach is to send <code>ViewModel</code> to <code>ListItem</code> to set <code>Commands</code>.
  Example
  somewhere we have method CreateListViewItem on the view, let's say on iOS.</li>
  </ol>
  
  <blockquote>
  <pre><code>private void InitTableView() {
      TableView.RegisterNibForCellReuse(ItemViewCell.Nib, ItemViewCell.Key);
  
      var source = new ObservableTableViewSource &lt;ItemModel&gt;
      {
          DataSource = ViewModel.Items,
          BindCellDelegate = (cell, viewModel, index) =&gt;
          {
              if (cell is ItemModel memberCell)
              {
                  memberCell.BindViewModel(viewModel);
                  memberCell.RemoveItem = (item) =&gt; ViewModel.RemoveItem;
              }
          }
      };
  
      TableView.Source = source;
  }
  </code></pre>
  </blockquote>
  
  <p><strong>Advantages</strong>: we no longer need to have <code>Commands</code> in <code>Model</code>, and we don't need to setup this <code>Commands</code> in <code>ViewModel</code>.</p>
  
  <p>Possibly, drawback is that we somehow need to have ViewModel reference.</p>
  
  <p>In WPF or UWP you have <code>DataContext</code>, you can binding directly to <code>ViewModel</code>.</p>
  
  <p>Which approach you use, maybe I miss something, and it would be perfect if you provide some examples or thoughts.
  Thanks.</p>