title: Create an ICommand Bindable property on Xamarin Forms
body: <p>I have a custom checkbox control that I created with an ICommand property and the corresponding bindable property (my checkbox is a Xamarin.Forms XAML Page), the code is:</p>
  
  <p><strong>CheckBox.xaml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Image x:Name="imgCheckBox"
         WidthRequest="20"
         HeightRequest="20"/&gt;
  
  &lt;Label x:Name="lblCheckBox"
         TextColor="Black"
         VerticalOptions="CenterAndExpand"/&gt;
  
  &lt;TapGestureRecognizer Tapped="OnCheckBoxTapped"/&gt;
  </code></pre>
  
  <p><strong>CheckBox.xaml.cs</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class CheckBox : ContentView
  {
      private static ImageSource uncheckedImage;
      private static ImageSource checkedImage;
  
      public CheckBox()
      {
          InitializeComponent();
          uncheckedImage = ImageSource.FromResource("cbUnchecked.png");
          checkedImage = ImageSource.FromResource("cbChecked.png");
          imgCheckBox.Source = uncheckedImage;
      }
  
      public static readonly BindableProperty IsCheckedProperty =
          BindableProperty.Create&lt;CheckBox, bool&gt;(
              checkbox =&gt; 
                  checkbox.IsChecked,
                  false,
                  propertyChanged: (bindable, oldValue, newValue) =&gt;
                  {
                      CheckBox checkbox = (CheckBox)bindable;
                      EventHandler&lt;bool&gt; eventHandler = checkbox.CheckedChanged;
                      if (eventHandler != null)
                      {
                          eventHandler(checkbox, newValue);
                      }
                  });        
  
      public bool IsChecked
      {
          set { SetValue(IsCheckedProperty, value); }
          get { return (bool)GetValue(IsCheckedProperty); }
      }
  
      void OnCheckBoxTapped(object sender, EventArgs args)
      {
          IsChecked = !IsChecked;
          if (IsChecked)
          {
              imgCheckBox.Source = checkedImage;
          }
          else
          {
              imgCheckBox.Source = uncheckedImage;
          }
      }
  
      public static readonly BindableProperty CheckBoxCommandProperty =
          BindableProperty.Create&lt;CheckBox, ICommand&gt;(
          checkbox =&gt;
              checkbox.CheckBoxCommand,
              null,
              BindingMode.TwoWay,
              propertyChanged: (bindable, oldValue, newValue) =&gt;
              {
                  CheckBox checkbox = (CheckBox)bindable;
                  EventHandler&lt;bool&gt; eventHandler = checkbox.CheckedChanged;
                  if (eventHandler != null)
                  {
                      eventHandler(checkbox, checkbox.IsChecked);
                  }
              });
  
      public event EventHandler&lt;bool&gt; CheckedChanged;
  
      public ICommand CheckBoxCommand
      {
          get { return (ICommand)GetValue(CheckBoxCommandProperty); }
          set { SetValue(CheckBoxCommandProperty, value); }
      }
  }
  </code></pre>
  
  <p>This checkbox implementation is on another Page called TermsAndConditionsPage, that is also a a Xamarin.Forms XAML Page, the code of the implementation is:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;toolkit:CheckBox Text="{Binding txtCheckBox}"
                    FontSize="Small"
                    CheckBoxCommand="{Binding OnCheckBoxTapChanged}"
                    IsChecked="{Binding IsCheckedChanged, Mode=TwoWay}"/&gt;
  
        &lt;Button Text="Next"
                Command="{Binding Next_OnClick}"
                IsEnabled="{Binding Next_IsEnabled}"
                HorizontalOptions="CenterAndExpand"
                Clicked="OnNextClicked"/&gt;
  </code></pre>
  
  <p>The Code Behind of this page is empty (Constructur with InitializeComponent()).
  I also have the ViewModel of this page with this code:</p>
  
  <p><strong>TermsAndConditionsViewModel.cs</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>private string _txtCheckBox;
  public string txtCheckBox
  { get { return _txtCheckBox; }
      set 
      { 
          _txtCheckBox = value;
          OnPropertyChanged("txtCheckBox");
      }
  }
  
  private bool _Next_IsEnabled;
  public bool Next_IsEnabled
  {
      get { return _Next_IsEnabled; }
      set
      {
          _Next_IsEnabled = value;
          OnPropertyChanged("Next_IsEnabled");
      }
  }
  
  private bool _IsCheckedChanged;
  public bool IsCheckedChanged
  {
      get { return _IsCheckedChanged; }
      set 
      { 
          _IsCheckedChanged = value;
          OnPropertyChanged("IsCheckedChanged");
      }
  }
  
  public ICommand Next_OnClick { get; set; }
  public ICommand OnCheckBoxTapChanged { get; set; }
  
  public TermsAndConditionsViewModel()
  {
      txtCheckBox = "I agree with the terms and conditions";
      Next_OnClick = new Command(NextClicked);
      OnCheckBoxTapChanged = new Command(CheckBoxTapped);
  }
  
  private void CheckBoxTapped()
  {
      if (IsCheckedChanged)
      { Next_IsEnabled = true; }
      else
      { Next_IsEnabled = false; }
  }
  
  private void NextClicked()
  { App.Current.MainPage = new Views.HelloWorld(); }
  
  #region INPC
  public void OnPropertyChanged(string propertyName)
  {
      if (PropertyChanged != null)
      { PropertyChanged(this, new PropertyChangedEventArgs(propertyName));          }                
  }
  public event PropertyChangedEventHandler PropertyChanged;
  #endregion  
  </code></pre>
  
  <p>Now, the question time: the problem I'm having is the CheckBoxTapped Command is not working, I mean, it doesn't do anything, although the checkbox image changes every time I touch it, it does not change the Next_IsEnabled property of my button. I'd like to know what I am missing here to make this command work properly.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>What I'm looking for is a Command that behaves similarly to the one that Buttons have.</p>
  
  <p>Thanks all for your time!</p>