title: Dynamically Adding event handlers to WPF
body: <p>I'm looking for help with an app I'm building. I've an xml file being read into an app. This XML is of the following structure:</p>
  
  <pre><code> `&lt;Tabs&gt;
    &lt;Tab&gt;
      &lt;Search name="ListSearch" Title="SearchHeader"&gt;
        &lt;Label Name="lblSchema"&gt;&lt;/Label&gt;
        &lt;ComboBox Name="comboxSchema" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableSchema}" SelectedValue="{Binding SelectedSchema}" /&gt;
        &lt;ComboBox Name="comboxList" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableList}" SelectedValue="{Binding SelectedList}" /&gt;
        &lt;Label Name="lblCriteria"&gt;&lt;/Label&gt;
        &lt;ComboBox Name="comboxFields" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableFields}" SelectedValue="{Binding SelectedField}" /&gt;
        &lt;ComboBox Name="comboxOperator" Visibility="Visible" IsEnabled="True" ItemSource="{Binding Operations}" SelectedValue="{Binding SelectedOperator}" /&gt;
        &lt;TextBox Name="txtBoxInputValue" Visibility="Visible" IsEnabled="True" Text="{Binding InputValue}" /&gt;
        &lt;CustomControl type="DatePicker"&gt;&lt;/CustomControl&gt;
        &lt;Button Name="btnAddQueryLine" Content="{Binding TextOnAddQueryButton}" Command="{Binding CamlAddQueryLine}" Action="Publish"&gt;&lt;/Button&gt;
        &lt;Button Name="btnPasteQueryLine" Content="{Binding TextOnPasteQueryButton}" Command="{Binding CamlPasteQueryLine}" Action="Preview"&gt;&lt;/Button&gt;
        &lt;Button Name="btnRemoveQueryLine" Content="{Binding TextOnRemoveQueryButton}" Command="{Binding CamlRemoveQueryLine}" Action="UnPublish"&gt;&lt;/Button&gt;
        &lt;Button Name="btnClearQuery" Content="{Binding TextOnClearQueryButton}" Command="{Binding CamlClearQuery}" Action="UnPreview"&gt;&lt;/Button&gt;
        &lt;Button Name="btnCamlSearch" Content="{Binding TextOnSearchQueryButton}" Command="{Binding CamlSearch}" Action="Meh"&gt;&lt;/Button&gt;
        &lt;Button Name="btnCloseSearch" Content="{Binding TextOnCloseQueryButton}" Command="{Binding CloseSearch}" Action="NewMeh"&gt;&lt;/Button&gt;
      &lt;/Search&gt;
    &lt;/Tab&gt;
  
    &lt;/Tabs&gt;` 
  </code></pre>
  
  <p>So I read in the xml, and use methods like this to add buttons etc to the ribbon:</p>
  
  <pre><code>private void AddButtonToGroup(string header,RibbonGroup group,string command,string action)
      {
           RibbonButton button = new RibbonButton();
           button.Label = header;
           button.Name = action;
  
           button.Click += new RoutedEventHandler(button_Click);
           group.Items.Add(button);
      }
  </code></pre>
  
  <p>with the following event handler:</p>
  
  <pre><code>void button_Click(object sender, RoutedEventArgs e)
      {
          Button clicked = (Button)sender;
  
          MessageBox.Show("Button Clicked!");
          MessageBox.Show("Performing Action:" + clicked.Name);
  
      }.
  </code></pre>
  
  <p>The problem I have, is that this isn't the requirement- the event handler is hard coded. Is there any way to create event handlers dynamically? Can anyone point me in the right direction?</p>