title: call a method of the binding class from a button click xamarin.
body: <p>So i have a View that i have bound to a List of Timer objects called Timers (custom class i have made), and in the view i have added a start and remove button. when a user clicks start i want them to be able to call the relevant timer object method startTimer() associated with the button. How can i do this?</p>
  
  <p>View code: </p>
  
  <pre><code>    &lt;ContentPage.Content&gt;
  &lt;StackLayout Orientation="Vertical"&gt;
  &lt;ListView ItemsSource="{Binding Timers, Mode=TwoWay}" SeparatorVisibility="None"&gt;
      &lt;ListView.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;ViewCell&gt;
                  &lt;StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal"&gt;
                      &lt;StackLayout Padding="10,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical"&gt;
                          &lt;Label Text="{Binding _name, Mode=TwoWay}" YAlign="Center"/&gt;
                          &lt;Label Text="{Binding _startTime, Mode=TwoWay}" YAlign="Center" FontSize="Small"/&gt;
                      &lt;/StackLayout&gt;
                      &lt;Button Text="Start" //button to associate with method//&gt;&lt;/Button&gt;
                      &lt;Button Text="Remove"&gt;&lt;/Button&gt;
                  &lt;/StackLayout&gt;
              &lt;/ViewCell&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListView.ItemTemplate&gt;
  &lt;/ListView&gt;
  &lt;Button Text="Add New" Clicked="AddNewTimer"/&gt;
  &lt;/StackLayout&gt;
  &lt;/ContentPage.Content&gt;
  </code></pre>
  
  <p>My Binded Class: </p>
  
  <pre><code>public class MainViewModel : INotifyPropertyChanged
  {
      public MainViewModel ()
      {
          Timers = DependencyService.Get&lt;ISaveAndLoad&gt; ().LoadTimers (); 
          if (Timers == null) {
              Timers = new ObservableCollection&lt;Timer&gt; (); 
          }
      }
  
      //When property changes notifys everything using it. 
      public event PropertyChangedEventHandler PropertyChanged;
      private void NotifyPropertyChanged(String propertyName = "")
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      private ObservableCollection&lt;Timer&gt; _timers;
      public ObservableCollection&lt;Timer&gt; Timers { 
          get { return _timers; } 
          set { 
              _timers = value;
              NotifyPropertyChanged ("Timers");
          } 
      }
  
      private string _title;
      public string Title{ 
          get{ 
              return _title; 
          }
          set{ 
              _title = value; 
              NotifyPropertyChanged ();
          }
      }
  }
  </code></pre>
  
  <p>And the timer class: </p>
  
  <pre><code>    public class Timer
  { 
      public int _startTime { get; set;} 
      public bool _hasStarted{ get; set; } 
      public string _name { get; set; }  
  
      public Timer (string name, int startTime, bool hasStarted = false)
      {
          _name = name; 
          _startTime = startTime; 
          _hasStarted = hasStarted; 
      }
  
      public void startTimer(){
          //do something here
      }
  }
  </code></pre>
  
  <p>Cheers. </p>