title: Toggle button (inside combobox) attach click handler
body: <p>I am rookie to XAML, WPF (first project ;-)). My goal is to apply a customized combobox declared inside App.xaml. However, if I then use the combobox inside the MainWindow.xaml, I don't know how to attach a click handler to the togglebutton which is anchored inside the combobox. If I add a Click property to the ToggleButton inside the App.xaml, I only succeeded to capture the event if I implement the handler inside App.cs.</p>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li><p>Refer to ToggleButton inside ComboBox element in MainWindow.xaml (I do not have a good intuition of how xaml works at this point ...), which results to some undesired visual effects on the ToggleButton and nothing more.</p>
  
  <pre><code>&lt;ComboBox x:Name="someComboBox" 
            Height="23"
            Margin="122,35,1.8,0"
            VerticalAlignment="Top"&gt;
  
           &lt;ToggleButton Click="toggleHandler_Click" /&gt;
  </code></pre></li>
  <li><p>Fetch ToggleButton from MainWindow.cs and then attach the handler, though the former with the approaches below results to null</p>
  
  <pre><code>    ToggleButton tBtn = ((ComboBox)someComboBox).FindName("MyToggleButton") as ToggleButton; // null
  
      ToggleButton tBtn = LogicalTreeHelper.FindLogicalNode(someComboBox, "MyToggleButton"); // null
  </code></pre></li>
  </ul>
  
  <p>Here is the customized combobox inside App.xaml, which I adopted as is </p>
  
  <pre><code>        &lt;Style TargetType="ComboBox"&gt;
              &lt;Setter Property="Validation.ErrorTemplate" Value="{x:Null}" /&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center" /&gt;
              &lt;Setter Property="OverridesDefaultStyle" Value="true" /&gt;
              &lt;Setter Property="IsEditable" Value="True"/&gt;
              &lt;Setter Property="SnapsToDevicePixels" Value="true" /&gt;
              &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /&gt;
              &lt;Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /&gt;
              &lt;Setter Property="ScrollViewer.CanContentScroll" Value="true" /&gt;
              &lt;Setter Property="Margin" Value="2" /&gt;
              &lt;Setter Property="MinHeight" Value="20" /&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="ComboBox"&gt;
                          &lt;Grid&gt;
                              &lt;VisualStateManager.VisualStateGroups&gt;
                                  &lt;VisualStateGroup x:Name="CommonStates"&gt;
                                      &lt;VisualState x:Name="Normal"/&gt;
                                      &lt;VisualState x:Name="MouseOver"/&gt;
                                      &lt;VisualState x:Name="Disabled"/&gt;
                                  &lt;/VisualStateGroup&gt;
                              &lt;/VisualStateManager.VisualStateGroups&gt;
                              &lt;ToggleButton x:Name="MyToggleButton"
                                            Grid.Column="2"
                                            ClickMode="Press"
                                            Focusable="false"
                                            IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                            Template="{StaticResource ComboBoxToggleButton}"/&gt;
  
                              &lt;ContentPresenter Margin="3,3,23,3" 
                                                Content="{TemplateBinding SelectionBoxItem}"
                                                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                HorizontalAlignment="Left"
                                                IsHitTestVisible="False"
                                                x:Name="ContentSite"
                                                VerticalAlignment="Center" /&gt;
  
                              &lt;TextBox Style="{x:Null}" 
                                       x:Name="PART_EditableTextBox" 
                                       Margin="3,3,23,3" 
                                       Background="Transparent"
                                       Focusable="True"
                                       HorizontalAlignment="Left"
                                       IsReadOnly="{TemplateBinding IsReadOnly}"
                                       Template="{StaticResource ComboBoxTextBox}" 
                                       VerticalAlignment="Center" 
                                       Visibility="Hidden" /&gt;
  
                              &lt;Popup AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" x:Name="Popup" Placement="Bottom" PopupAnimation="Fade"&gt;
                                  &lt;Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True"&gt;
                                      &lt;Border x:Name="DropDownBorder" Background="White" BorderBrush="{StaticResource BorderDarkMainBrush}" BorderThickness="1" CornerRadius="0" /&gt;
                                      &lt;ScrollViewer Margin="2" SnapsToDevicePixels="True"&gt;
                                          &lt;StackPanel KeyboardNavigation.DirectionalNavigation="Contained" IsItemsHost="True" TextBlock.Foreground="Black" /&gt;
                                      &lt;/ScrollViewer&gt;
                                  &lt;/Grid&gt;
                              &lt;/Popup&gt;
                          &lt;/Grid&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="HasItems" Value="false"&gt;
                                  &lt;Setter Property="MinHeight" TargetName="DropDownBorder" Value="95" /&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="IsGrouping" Value="true"&gt;
                                  &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false" /&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="IsEditable" Value="true"&gt;
                                  &lt;Setter Property="IsTabStop" Value="false" /&gt;
                                  &lt;Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible" /&gt;
                                  &lt;Setter Property="Visibility" TargetName="ContentSite" Value="Hidden" /&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
  
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="IsFocused" Value="True"&gt;
                      &lt;Setter Property="Background" Value="Transparent" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
  </code></pre>