title: WPF CommandParameter is NULL first time CanExecute is called
body: <p>I have run into an issue with WPF and Commands that are bound to a Button inside the DataTemplate of an ItemsControl. The scenario is quite straight forward. The ItemsControl is bound to a list of objects, and I want to be able to remove each object in the list by clicking a Button. The Button executes a Command, and the Command takes care of the deletion. The CommandParameter is bound to the Object I want to delete. That way I know what the user clicked. A user should only be able to delete their "own" objects - so I need to do some checks in the "CanExecute" call of the Command to verify that the user has the right permissions.</p>
  
  <p>The problem is that the parameter passed to CanExecute is NULL the first time it's called - so I can't run the logic to enable/disable the command. However, if I make it allways enabled, and then click the button to execute the command, the CommandParameter is passed in correctly. So that means that the binding against the CommandParameter is working.</p>
  
  <p>The XAML for the ItemsControl and the DataTemplate looks like this:</p>
  
  <pre><code>&lt;ItemsControl 
      x:Name="commentsList"
      ItemsSource="{Binding Path=SharedDataItemPM.Comments}"
      Width="Auto" Height="Auto"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Button                             
                      Content="Delete"
                      FontSize="10"
                      Command="{Binding Path=DataContext.DeleteCommentCommand, ElementName=commentsList}" 
                      CommandParameter="{Binding}" /&gt;
              &lt;/StackPanel&gt;                       
           &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>So as you can see I have a list of Comments objects. I want the CommandParameter of the DeleteCommentCommand to be bound to the Command object.</p>
  
  <p>So I guess my question is: have anyone experienced this problem before? CanExecute gets called on my Command, but the parameter is always NULL the first time - why is that?</p>
  
  <p><strong>Update:</strong> I was able to narrow the problem down a little. I added an empty Debug ValueConverter so that I could output a message when the CommandParameter is data bound. Turns out the problem is that the CanExecute method is executed before the CommandParameter is bound to the button. I have tried to set the CommandParameter before the Command (like suggested) - but it still doesn't work. Any tips on how to control it.</p>
  
  <p><strong>Update2:</strong> Is there any way to detect when the binding is "done", so that I can force re-evaluation of the command? Also - is it a problem that I have multiple Buttons (one for each item in the ItemsControl) that bind to the same instance of a Command-object?</p>
  
  <p><strong>Update3:</strong> I have uploaded a reproduction of the bug to my SkyDrive: <a href="http://cid-1a08c11c407c0d8e.skydrive.live.com/self.aspx/Code%20samples/CommandParameterBinding.zip" rel="noreferrer">http://cid-1a08c11c407c0d8e.skydrive.live.com/self.aspx/Code%20samples/CommandParameterBinding.zip</a></p>