title: Getting ListBoxItem from item template control event
body: <p>In the WPF XAML code below, if I am in the SelectTaskItemClick event for the templated Button, how do I get the ListBoxItem ItemSource object that is currently selected? </p>
  
  <pre><code>    &lt;!-- ListBox ITEMS --&gt;
      &lt;TaskDash:ListBoxWithAddRemove x:Name="listBoxItems" Grid.Row="1" Grid.Column="3" Grid.RowSpan="3"
          ItemsSource="{Binding}"&gt;
          &lt;!--ItemsSource="{Binding}" DisplayMemberPath="Description"&gt;--&gt;
          &lt;Style TargetType="ListBoxItem"&gt;
              &lt;Setter Property="IsSelected" Value="{Binding Path=Selected}"/&gt;
          &lt;/Style&gt;
          &lt;TaskDash:ListBoxWithAddRemove.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;DockPanel&gt;
                      &lt;Button DockPanel.Dock="Left" Click="SelectTaskItemClick"&gt;SELECT&lt;/Button&gt;
                      &lt;TextBox DockPanel.Dock="Left" Name="EditableDescription" Text="{Binding Description}" Height="25" Width="100" /&gt;
                      &lt;Button DockPanel.Dock="Left" Click="EditTaskItemClick"&gt;EDIT&lt;/Button&gt;
                  &lt;/DockPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/TaskDash:ListBoxWithAddRemove.ItemTemplate&gt;
      &lt;/TaskDash:ListBoxWithAddRemove&gt;
  </code></pre>
  
  <p>If I try to get the Parent or TemplateParent, it gives me the ContentPresenter or Style or something similar. </p>
  
  <pre><code>    private void SelectTaskItemClick(object sender, RoutedEventArgs e)
      {
          Button taskItemButton = (Button) e.OriginalSource;
          ContentPresenter taskItem = (ContentPresenter) taskItemButton.TemplatedParent;
          taskItem = (ContentPresenter)taskItemButton.TemplatedParent;
          Style taskItem2 = taskItem.TemplatedParent;
          taskItem2 = taskItem.TemplatedParent;
          DependencyObject taskItem3 = taskItem2.Parent;
          //DependencyObject taskItem3 = taskItem2.TemplatedParent;
          //TaskItem taskItemObj = taskItem2;
      }
  </code></pre>
  
  <p>In the code above, I'm guessing it is grabbing that from App.XAML where that custom ListBoxWithAddRemove control is defined. How do I traverse the actual form's XAML instead [the first code shown above]?</p>
  
  <pre><code>&lt;Style x:Key="{x:Type TaskDash:ListBoxWithAddRemove}" TargetType="{x:Type     TaskDash:ListBoxWithAddRemove}"&gt;
              &lt;Setter Property="Margin" Value="3" /&gt;
              &lt;Setter Property="SnapsToDevicePixels" Value="True"/&gt;
              &lt;Setter Property="OverridesDefaultStyle" Value="True"/&gt;
              &lt;Setter Property="KeyboardNavigation.TabNavigation" Value="None"/&gt;
              &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt;
              &lt;Setter Property="MinWidth" Value="120"/&gt;
              &lt;Setter Property="MinHeight" Value="20"/&gt;
              &lt;Setter Property="AllowDrop" Value="true"/&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type TaskDash:ListBoxWithAddRemove}"&gt;
                          &lt;Grid&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition Height="25" /&gt;
                                  &lt;RowDefinition Height="*" /&gt;
                              &lt;/Grid.RowDefinitions&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="*" /&gt;
                                  &lt;ColumnDefinition Width="*" /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
  
                              &lt;Button Grid.Column="0" Grid.Row="0" 
                                      Click="DeleteControlClick"&gt;Delete&lt;/Button&gt;
                              &lt;Button Grid.Column="1" Grid.Row="0" 
                                      Click="AddControlClick"&gt;Add&lt;/Button&gt;
                              &lt;Border 
                                  Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
                                    Name="Border" 
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    BorderBrush="{StaticResource SolidBorderBrush}"
                                    BorderThickness="1"
                                    CornerRadius="2"&gt;
                                  &lt;ScrollViewer 
                                      Margin="0"
                                      Focusable="false"&gt;
                                      &lt;StackPanel Margin="0" IsItemsHost="True" /&gt;
                                  &lt;/ScrollViewer&gt;
                              &lt;/Border&gt;
                          &lt;/Grid&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  </code></pre>