title: MVVM Light: Bind event to command in code behind
body: <p>I am using MVVM light to bind events on UIElements in my View to ICommands in my view model, and this is working very well.</p>
  
  <p>However - I have a situation where I have to create some controls on the fly, and I'm doing this in the code behind on the view as this seems the best place to do it, and it is after all UI code. As I am generating these controls in the code, I also need to create the event bindings in the code and, despite several attempts, I have not acheived the desired result.</p>
  
  <p>Has anybody done this or does anybody know how to?</p>
  
  <p>Any help appreciated!</p>
  
  <hr>
  
  <p>thanks for your answers and comments. Matt is right - the question is about how to use EventToCommand in code instead of XAMl. Matt - I do need to generate the controls at runtime because I never know until that point what the controls will be. The controls are on a ribbon, and the ribbon is context sensitive, i.e. whenever the main view is changed the controls on the ribbon change with it and need to be generated.</p>
  
  <p>I have found a solution using attached properties, but I would still like to know how (and if) this can be done with MVVM Light.</p>