title: How do I pass a variable as a CommandParameter
body: <p>I'm trying to send a variable from the ViewModel as a parameter to a command.  The command looks like this:</p>
  
  <pre><code>public class EditPersonCommand : ICommand
  {
    private bool _CanExecute = false;
  
    public bool CanExecute(object parameter)
    {
       PersonModel p = parameter as PersonModel;
       CanExecuteProperty = (p != null) &amp;&amp; (p.Age &gt; 0);
       return CanExecuteProperty;
    }
  
    public event EventHandler CanExecuteChanged;
  
    public void Execute(object parameter) { }
  
    private bool CanExecuteProperty
    {
       get { return _CanExecute; }
       set
       {
          if (_CanExecute != value)
          {
             _CanExecute = value;
             EventHandler can_execute = CanExecuteChanged;
             if (can_execute != null)
             {
                can_execute.Invoke(this, EventArgs.Empty);
             }
          }
       }
    }
  }
  </code></pre>
  
  <p>The ViewModel looks like this:</p>
  
  <pre><code>public class PersonViewModel : ViewModelBase
  {
    private PersonModel _PersonModel;
    private EditPersonCommand _EditPersonCommand;
  
    ///&lt;remarks&gt;
    /// must use the parameterless constructor to satisfy &lt;Window.Resources&gt;
    ///&lt;/remarks&gt;
    public PersonViewModel()
       : this(new PersonModel())
    {
  
    }
  
    public PersonViewModel(PersonModel personModel)
    {
       _PersonModel = personModel;
    }
  
    public ICommand EditPersonCommand
    {
       get
       {
          if (_EditPersonCommand == null)
          {
             _EditPersonCommand = new EditPersonCommand();
          }
          return _EditPersonCommand;
       }
    }
  }
  </code></pre>
  
  <p>The xaml looks like this:</p>
  
  <pre><code>&lt;Button Content="Edit" HorizontalAlignment="Right" Height="20" Width="80"
     Command="{Binding EditPersonCommand}" 
     CommandParameter="{Binding _PersonModel}" /&gt;
  </code></pre>
  
  <p>I've tried creating a property in the ViewModel instead of using the private local variable name, but that didnt work either.  The <code>object parameter</code> always shows <code>null</code> in the call to <code>CanExecute</code> and the button is never enabled.  If I change the <code>CommandParameter</code> value to <code>Hello</code>, then I receive <code>Hello</code> in the call to <code>CanExecute</code>, so I'm not sure why the variable doesnt work.  Any help would be appreciated.</p>
  
  <p><strong>Update:</strong>  I've also tried making a public property to the model (which I dont really want to expose the model, but just tried it to see if it works, but it doesnt).</p>
  
  <pre><code>// Added this to the ViewModel
  public PersonModel PersonModelProp
  {
    get
    {
       return _PersonModel;
    }
    set
    {
       _PersonModel = value;
       OnPropertyChanged("PersonModelProp");
    }
  }
  </code></pre>
  
  <p>And changed the xaml to this:</p>
  
  <pre><code>&lt;Button Content="Edit" HorizontalAlignment="Right" Height="20"  Width="80"
     Command="{Binding EditPersonCommand}" 
     CommandParameter="{Binding PersonModelProp}" /&gt;
  </code></pre>
  
  <p>But still no luck.  The ViewModel does implement <code>INotifyPropertyChanged</code></p>