title: Using custom class in XAML so it's properties serves as binding destination
body: <p>First of all sorry for my "pidgin" english, :(</p>
  
  <p>Application was built on MVVM pattern. ViewModel exposes its functionality via commands &amp; has special command - DispatchCommand which receives (in CommandParameter) pair of other Command/Parameter &amp; executes received Command with received Parameter. The feature (rather unimportant for topic purposes) is that the DispatcherCommand is executing in primary application thread, other ViewModel commands (invoked by DispatcherCommand) is asynchronous, and each of them executes its Execute() delegate in separate thread. Such approach (indirect execution of async ViewModel commands via DispatchCommand) used in order to manage inner ViewModel state (for example, DispatchCommands refuses invocation of particular async command while other async command executes &amp; so on).</p>
  
  <p>In code it looks like:</p>
  
  <pre><code>// Dispatcher command
  public class DispatcherCommand : ICommand { }
  
  // async command
  public interface IAsyncCommand : ICommand { }
  public class AsyncCommand : IAsyncCommand { }
  
  // view model
  public class AsyncModel
  {
      public DispatcherCommand Dispatcher { get {...} }
  
      // async commands - current model capabilities
      public AsyncCommand FirstCapability { get {...} }
      public AsyncCommand SecondCapability { get {...} }
      ...
  }
  
  // using example
  AsyncModel model = new AsyncModel();
  model.Dispatcher.Execute( 
     new { Command = model.FirstCapability, Parameter = "string param" });
  </code></pre>
  
  <p>I want provide access to async commands in the XAML in the same way:</p>
  
  <pre><code>&lt;Button Command="{Binding Path=DispatcherCommand}"&gt;
      &lt;Button.CommandParameter&gt;
          &lt;local:CurrentCommandParameters Command="{Binding Path=FirstCapability}"
                                          Parameter="string param"/&gt;
      &lt;/Button.CommandParameter&gt;Use first capability&lt;/Button&gt;
  </code></pre>
  
  <p>where </p>
  
  <pre><code>    // DispatcherCommand parameter class
  public class DispatcherCommandParameters : DependencyObject
  {
      // target async command
      public IAsyncCommand Command
      {
          set { SetValue(CommandProperty, value); }
          get { return (IAsyncCommand)GetValue(CommandProperty); }
      }
  
      // its parameter
      public object Parameter
      {
          set { SetValue(ParameterProperty, value); }
          get { return GetValue(ParameterProperty); }
      }
  
      public static readonly DependencyProperty CommandProperty;
      public static readonly DependencyProperty ParameterProperty;
  
      static CurrentCommandParameters()
      {
          CommandProperty = DependencyProperty.Register("Command",
              typeof(IAsyncCommand),
              typeof(CurrentCommandParameters),
              new PropertyMetadata());
  
          ParameterProperty = DependencyProperty.Register("Parameter",
              typeof(object),
              typeof(CurrentCommandParameters),
              new PropertyMetadata());
      }
  }
  </code></pre>
  
  <p>but it's not working. DispatcherCommand.Execute(object parameter) receives an instance of DispatcherCommandParameters class, but its Command property is null.</p>
  
  <p>What can be done in this situation?</p>
  
  <hr>
  
  <p>In window constructor the same binding, as failed in xaml, created successfully:</p>
  
  <pre><code>&lt;Button Name="StupidButton" Command="{Binding Path=DispatcherCommand}"&gt;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
  
      // set up data context
      AsyncModel model = new AsyncModel();
      this.DataContext = model;
  
      // set up binding
      DispatcherCommandParameters dcp = new DispatcherCommandParameters();
      dcp.Parameter = "string parameter value set in code";
      Binding myBinding = new Binding("FirstCapability");
      myBinding.Source = model;
      BindingExpressionBase be = BindingOperations.SetBinding(dcp, 
          DispatcherCommandParameters.CommandProperty, myBinding);
      StupidButton.CommandParameter = dcp;            
  
      // now when "StupidButton" is pressed, 
      // DispatcherCommand.Execute(object parameter)
      // receives correct reference to AsyncModel.FirstCapability command in 
      // ((DispatcherCommandParameters)parameter).Command property
  }
  </code></pre>