title: WPF: Using a button as a DataTemplate, how to add click event
body: <p>I have a ItemsControl with a collection of objects. I wan't to be able to click the object and then get a panels with more info.</p>
  
  <p>So I decided to style the DataTemplate for the items in the ItemsControl as a button and that seems to work fine. However I have no idea how to set the click event of this button in the style. It says I should use a EventSetter but I can't get that to work.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>  &lt;Style TargetType="Expander" &gt;
              &lt;Style.Resources&gt;
                  &lt;Style TargetType="ItemsControl" &gt;
                      &lt;Setter Property="Template" &gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="ItemsControl"&gt;
                                  &lt;Border BorderThickness="0,1,0,1" BorderBrush="{StaticResource DarkColorBrush}" &gt;
                                      &lt;ScrollViewer Margin="0" VerticalScrollBarVisibility="Auto"
                                                    Focusable="false"&gt;
                                          &lt;StackPanel Margin="2" IsItemsHost="True" /&gt;
                                      &lt;/ScrollViewer&gt;
                                  &lt;/Border&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                      &lt;Setter Property="ItemTemplate" &gt;
                          &lt;Setter.Value&gt;
                              &lt;DataTemplate DataType="{x:Type data:CompanyViewModel}" &gt;
                                  &lt;Button&gt;
                                      &lt;Button.Resources&gt;
                                          &lt;Style TargetType="Button"&gt;
                                              &lt;Setter Property="Template"&gt;
                                                  &lt;Setter.Value&gt;
                                                      &lt;ControlTemplate TargetType="Button"&gt;
                                                          &lt;Border Name="Bd" BorderBrush="{StaticResource DarkColorBrush}"
                                                                  BorderThickness="1"
                                                                  Margin="5"
                                                                  CornerRadius="8"&gt;
  
                                                              &lt;Border.Background&gt;
                                                                  &lt;!-- Removed for brevity --&gt;
                                                              &lt;/Border.Background&gt;
  
                                                              &lt;StackPanel Orientation="Vertical"&gt;
                                                                  &lt;TextBlock Margin="5" Text="{Binding Path=Name}" Style="{StaticResource MenuText}" FontSize="16" HorizontalAlignment="Center" /&gt;
                                                                  &lt;TextBlock Margin="5,0,5,5" Text="{Binding Path=Code, StringFormat=Kt. {0}}" Style="{StaticResource MenuText}" HorizontalAlignment="Center" /&gt;
                                                              &lt;/StackPanel&gt;
                                                          &lt;/Border&gt;
  
                                                          &lt;ControlTemplate.Triggers&gt;
                                                              &lt;Trigger Property="IsMouseOver" Value="true"&gt;
                                                                  &lt;Setter TargetName="Bd" Property="Background"&gt;
                                                                      &lt;Setter.Value&gt;
                                                                          &lt;!-- Removed for brevity --&gt;
                                                                      &lt;/Setter.Value&gt;
                                                                  &lt;/Setter&gt;
                                                              &lt;/Trigger&gt;
                                                              &lt;Trigger Property="Button.IsPressed" Value="true"&gt;
                                                                  &lt;Setter TargetName="Bd" Property="Background"&gt;
                                                                      &lt;Setter.Value&gt;
                                                                          &lt;!-- Removed for brevity --&gt;
                                                                      &lt;/Setter.Value&gt;
                                                                  &lt;/Setter&gt;
                                                              &lt;/Trigger&gt;
                                                          &lt;/ControlTemplate.Triggers&gt;
                                                      &lt;/ControlTemplate&gt;
                                                  &lt;/Setter.Value&gt;
                                              &lt;/Setter&gt;
                                          &lt;/Style&gt;
                                      &lt;/Button.Resources&gt;
                                  &lt;/Button&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/Style.Resources&gt;
              &lt;Setter Property="Template" &gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="Expander"&gt;
                          &lt;Grid&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="*" /&gt;
                                  &lt;ColumnDefinition Width="30" /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;ToggleButton Grid.Column="1"
                                            IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,
                                            RelativeSource={RelativeSource TemplatedParent}}" /&gt;
                              &lt;ContentPresenter Name="Content" Grid.Column="0" /&gt;
                          &lt;/Grid&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="IsExpanded" Value="false"&gt;
                                  &lt;Setter TargetName="Content" Property="Visibility" Value="Collapsed" /&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  </code></pre>
  
  <p>Decided to add what I wanted to accomplish with the button click:</p>
  
  <pre><code>&lt;Button Click="CompanyClick" /&gt;
  </code></pre>
  
  <p>CompanyClick being defined in the code behind.</p>