title: Is there a way to programatically bind to InputBinding Command property?
body: <p>I know it's a generic title, but my question is specific.  I think it will boil down to a question of practice.  So, I have the following code:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      InitializeComponent();
      MyViewModel viewModel = new MyViewModel();
      this.myGrid.DataContext = viewModel;
  }
  
  public class MyViewModel
  {
      public ICommand SomeCommandProperty { get { return this.someCommandProperty; }}
  }
  
  public class ComponentCollection : Panel
  {
      public ComponentCollection()
      {
          for (int i = 0; i &lt; n; i++)
          {
              this.Children.Add(new Component());
          }
      }
  }
  
  public class Component : UIElement
  {
      public Component()
      {
          this.InputBindings.Add(new MouseBinding(SomeCommandProperty, new MouseGesture(MouseAction.LeftClick)));
      }
  }
  </code></pre>
  
  <p>I could easily aggregate the ViewModel that owns SomeCommandProperty into the Component class, but I'm currently waiving that option assuming there is another way.</p>
  
  <p>Component is a child of ComponentCollection which is child of a Grid which DataContext is MyViewModel. ComponentCollection as the name suggests contains a collection of Components. </p>
  
  <pre><code>&lt;Grid Name="myGrid"&gt;
      &lt;someNamespace:ComponentCollection x:Name="componentCollection"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>It's the same scenario as the XAML below, but with TextBlock.  I guess I'm trying to replicate what's being done in the XAML below programatically.  Again, Component's top most ancestor's DataContext is set to ViewModel.</p>
  
  <pre><code>&lt;Grid Name="myGrid"&gt;
      &lt;TextBlock Text="SomeText"&gt;
          &lt;TextBlock.InputBindings&gt;
              &lt;MouseBinding Command="{Binding SomeCommandProperty}" MouseAction="LeftClick" /&gt;
          &lt;/TextBlock.InputBindings&gt;
      &lt;/TextBlock&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Update 1</strong></p>
  
  <p>Basically, I have a custom control which inherit from a Panel which children are a collection of Component.  It's not a hack, like I've mentioned, I could directly have access to <strong><em>SomeCommandProperty</em></strong> If I aggregate the ViewModel into Component.  Doing so, however, feels icky.  That is, having direct access to ViewModel from a Model.</p>
  
  <p>I guess the question I'm asking is. Given the situation that Component's parent UIElement's DataContext is set to MyViewModel, is it possible to access <strong>SomeCommandProperty</strong> without <strong><em>Component</em></strong> owning a reference to the <strong><em>MyViewModel</em></strong> that owns <strong>SomeCommandProperty</strong>? Programatically, that is.</p>
  
  <p>Using ItemsControl doesn't change the fact that I still need to bind SomeCommandProperty to each Items.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>See code above.</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>Apparently, there isn't a mechanism I know of that will set the binding on the Command property of an InputBinding.</p>
  
  <p>For example if my Component class were to Inherit from ButtonBase instead of UIElement, I would have the Command property to which I could easily set the binding <strong>programatically</strong> using FrameWorkElement's SetBinding.  Unfortunately, I can't do this with InputBinding's Command property.</p>
  
  <pre><code>public class Component : ButtonBase
  {
      public Component()
      {
          System.Windows.Data.Binding binding = new System.Windows.Data.Binding
          {
              RelativeSource = new System.Windows.Data.RelativeSource(System.Windows.Data.RelativeSourceMode.FindAncestor, typeof(ComponentCollection), 1 ),
              Path = new PropertyPath("DataContext.SomeCommandProperty")
          };
  
          // I can do this.
          this.SetBinding(this.CommandProperty, binding);
  
          // But I want to do something like below.  Note: It's a pseudo code.
          MouseBinding mouseBinding = new MouseBinding();
          mouseBinding.SetBinding(mouseBinding.CommandProperty, binding);
          this.InputBindings.Add(mouseBinding);
      }
  }
  </code></pre>
  
  <p><strong>Update 4</strong></p>
  
  <p>BindingOperations.SetBinding can be used on Objects that don't have direct access to SetBinding.</p>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>        MouseBinding mouseBinding = new MouseBinding();
          BindingOperations.SetBinding(mouseBinding, MouseBinding.CommandProperty, binding);
          this.InputBindings.Add(mouseBinding);
  </code></pre>