title: Combobox with custom dropdown(Popup) template and more
body: <p>I am having a problem hooking up some events in my custom dropdown or Popup template in my combobox.</p>
  
  <p>This custom template by the way looks like the one in IE 10. Here's the picture
  <img src="https://i.stack.imgur.com/tLZD9.jpg" alt="enter image description here">
  I got the partial look (the one below is mine). But I'm having a problem deleting an item in history list. Below is my PART_Popup template</p>
  
  <pre><code>&lt;Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"&gt;
      &lt;Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MinWidth="{Binding ActualWidth, ElementName=Placement}"&gt;
          &lt;Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"&gt;
              &lt;!--&lt;ScrollViewer&gt;--&gt;
              &lt;Grid&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="*" /&gt;
                      &lt;RowDefinition Height="Auto" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;StackPanel&gt;
                      &lt;ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
  
                      &lt;Border Padding="5"&gt;
                          &lt;StackPanel&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                      &lt;ColumnDefinition Width="*" /&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;TextBlock Text="History" Foreground="{StaticResource SeparatorLine}" /&gt;
                                  &lt;Border Height="2" Grid.Column="1" Margin="5,0,0,0" BorderBrush="{StaticResource SeparatorLine}" BorderThickness="0.5" /&gt;
                              &lt;/Grid&gt;
                              &lt;ListBox x:Name="listHistory"  BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch"&gt;
                                  &lt;ListBox.ItemTemplate&gt;
                                      &lt;DataTemplate&gt;
                                          &lt;Grid&gt;
                                              &lt;TextBlock Text="{Binding }" /&gt;
                                              &lt;Button Grid.Column="1" HorizontalAlignment="Right" x:Name="btnDeleteHistoryItem" Content="r" FontFamily="Marlett" Style="{DynamicResource ButtonStyle}" Visibility="Hidden" Opacity="0.75" /&gt;
                                          &lt;/Grid&gt;
  
                                          &lt;DataTemplate.Triggers&gt;
                                              &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                                                  &lt;Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" /&gt;
                                              &lt;/Trigger&gt;
                                          &lt;/DataTemplate.Triggers&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/ListBox.ItemTemplate&gt;
                              &lt;/ListBox&gt;
                      &lt;/StackPanel&gt;
                      &lt;/Border&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;Border Grid.Row="1" BorderBrush="{StaticResource SeparatorLine}" BorderThickness="0,1,0,0" Padding="5" Height="30"&gt;
                      &lt;Border.Background&gt;
                          &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                              &lt;GradientStop Color="#33000000" Offset="0"/&gt;
                              &lt;GradientStop Offset="1"/&gt;
                              &lt;GradientStop Offset="0.375"/&gt;
                          &lt;/LinearGradientBrush&gt;
                      &lt;/Border.Background&gt;
  
                      &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Right"&gt;
                          &lt;Button x:Name="btnClearHistory" Content=" Clear History " Style="{DynamicResource ButtonStyle}" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Border&gt;
              &lt;/Grid&gt;
              &lt;!--&lt;/ScrollViewer&gt;--&gt;
          &lt;/Border&gt;
      &lt;/Themes:SystemDropShadowChrome&gt;
  &lt;/Popup&gt;
  </code></pre>
  
  <p>alright, in the <strong>Listbox(listHistory)</strong> I have a button there called <strong>btnDeleteHistoryItem</strong> and I cannot hook it. It's returning a Null error in my code here</p>
  
  <pre><code>protected override void OnSourceInitialized(EventArgs e)
  {
      ListBox lb = (ListBox)cbSearch.Template.FindName("listHistory", cbSearch);
      lb.ItemsSource = this.SearchHistory;
      lb.SelectionChanged += cbResults_SelectionChanged;
  
      Button btnDeleteHistoryItem = (Button)lb.Template.FindName("btnDeleteHistoryItem", lb);
      // if (btnDeleteHistoryItem != null)
      {
          btnDeleteHistoryItem.Click += DeleteHistoryItem_Click;
      }
  
      // or --------------
  
      Button btnDeleteHistoryItem = (Button)cbSearch.Template.FindName("btnDeleteHistoryItem", cbSearch);
      // if (btnDeleteHistoryItem != null)
      {
          btnDeleteHistoryItem.Click += DeleteHistoryItem_Click;
      }
  }
  </code></pre>
  
  <p>Coding is a little classic here, so no MVVM implemented in hooking with events. So the problem is this <strong>Button btnDeleteHistoryItem = (Button)lb.Template.FindName("btnDeleteHistoryItem", lb);</strong> or <strong>Button btnDeleteHistoryItem = (Button)cbSearch.Template.FindName("btnDeleteHistoryItem", cbSearch);</strong>
   line where it cannot find that btnDeleteHistoryItem.</p>
  
  <p>How can I resolve this?</p>
  
  <p>-- UPDATE --</p>
  
  <pre><code>lb.Loaded += (a, b) =&gt;
  {
      Button btnDeleteHistoryItem = (Button)lb.Template.FindName("btnDeleteHistoryItem", lb);
      //if (btnDeleteHistoryItem != null)
      {
          btnDeleteHistoryItem.Click += DeleteHistoryItem_Click;
      }
  };
  </code></pre>
  
  <p>doesn't work either</p>