title: Dynamically add KeyBindings in WPF
body: <p>Is it possible to dynamically define KeyBindings based on a bound data source? I have a screen with a grid and I allow users to save various layouts for it. I currently bind the grids context menu to the layout names (via the ViewModel), allowing them to switch layouts via the menu.</p>
  
  <p>However, I would like to associate each layout with a shortcut key. As the shortcut keys are defined by the user I can't simply add a number of <code>&lt;KeyBinding&gt;</code> elements in the window XAML. Another issue is the binding would need to supply the name of the layout as a command parameter.</p>
  
  <p>Is there any way to dynamically create a series of <code>&lt;KeyBinding&gt;</code> elements from a dynamic source?</p>
  
  <p>As a test I have added the bindings statically to my view XAML and they work fine, but this was only to test my concept:</p>
  
  <pre><code>&lt;UserControl.InputBindings&gt;
      &lt;KeyBinding Key="F7" Command="{Binding MyCommand}" CommandParameter="My Layout Name"/&gt;
      &lt;KeyBinding Key="F8" Command="{Binding MyCommand}" CommandParameter="My Other Layout Name"/&gt;
  &lt;/UserControl.InputBindings&gt;
  </code></pre>