title: How do I pass the Button as CommandParameter from XAML in a Xamarin.Forms Page?
body: <p>I would like to pass a <code>Xamarin.Forms.Button</code> in it's own <code>Command</code> as the <code>CommandParameter</code> to my ViewModel. I know how to achieve this from the code behind e.g. ... </p>
  
  <p><strong>XAML</strong> <em>(with most properties missed out for brevity)</em></p>
  
  <pre><code>&lt;Button x:Name="myButton"
      Text="My Button"
      Command="{Binding ButtonClickCommand}"/&gt;
  </code></pre>
  
  <p><strong>XAML.cs</strong></p>
  
  <pre><code>public partial class MyTestPage
  {
      public MyTestPage()
      {
          InitializeComponent();
  
          myButton.CommandParameter = myButton;
      }
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>public class MyViewModel : ViewModelBase
  {
      public MyViewModel()
      {
          ButtonClickCommand = new Command(
              (parameter) =&gt;
              {
                  var view = parameter as Xamarin.Forms.Button;
                  if (view != null)
                  {
                      // Do Stuff
                  }
              });
      }
  
      public ICommand ButtonClickCommand { get; private set; }
  }
  </code></pre>
  
  <p>... BUT is it possible to declare the <code>CommandParameter</code> in the XAML itself? Or in other words what is the binding syntax to set the parameter to the button itself?</p>
  
  <pre><code>&lt;Button x:Name="myButton"
          Text="My Button"
          Command="{Binding ButtonClickCommand}"
          CommandParameter="{[WHAT WOULD GO HERE]}"/&gt;
  </code></pre>
  
  <p>btw I've already tried <code>CommandParameter="{Binding RelativeSource={RelativeSource Self}}"</code> and that didn't work.</p>
  
  <p>Thanks,</p>