title: WPF CommandParameter Binding Problem
body: <p>I'm having some trouble understanding how command parameter binding works.</p>
  
  <p>When I create an instance of the widget class before the call to InitializeComponent it seems to work fine.  Modifications to the parameter(Widget) in the ExecuteCommand function will be "applied" to _widget.  This is the behavior I expected.  </p>
  
  <p>If the instance of _widget is created after InitializeComponent, I get null reference exceptions for e.Parameter in the ExecuteCommand function.</p>
  
  <p>Why is this?  How do I make this work with  MVP pattern, where the bound object may get created after the view is created?</p>
  
  <pre><code>public partial class WidgetView : Window
  {
      RoutedCommand _doSomethingCommand = new RoutedCommand();
  
      Widget _widget;
  
      public WidgetView()
      {
          _widget = new Widget();
          InitializeComponent();
          this.CommandBindings.Add(new CommandBinding(DoSomethingCommand, ExecuteCommand, CanExecuteCommand));
      }
  
      public Widget TestWidget
      {
          get { return _widget; }
          set { _widget = value; }
      }
  
      public RoutedCommand DoSomethingCommand
      {
          get { return _doSomethingCommand; }
      }
  
      private static void CanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
      {
          if (e.Parameter == null)
              e.CanExecute = true;
          else
          {
              e.CanExecute = ((Widget)e.Parameter).Count &lt; 2;
          }
      }
  
      private static void ExecuteCommand(object sender, ExecutedRoutedEventArgs e)
      {
          ((Widget)e.Parameter).DoSomething();
      }
  }
  
  
  
  &lt;Window x:Class="CommandParameterTest.WidgetView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="WidgetView" Height="300" Width="300"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
      &lt;StackPanel&gt;
          &lt;Button Name="_Button" Command="{Binding DoSomethingCommand}"
               CommandParameter="{Binding TestWidget}"&gt;Do Something&lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  
  
  public class Widget
  {
      public int Count = 0;
      public void DoSomething()
      {
          Count++;
      }
  }
  </code></pre>