title: How to automatically select a WPF ListViewItem
body: <p>I have a ListView in my WPF UserControl using an ItemTemplate to display the items. Within the template is a button. When I select one item and then click on the button of another item, the previously selected item is still selected. I wonder how to automatically select the item the button is in when the button is clicked.</p>
  
  <p><strong>Xaml</strong></p>
  
  <pre><code>&lt;UserControl.Resources&gt;
    &lt;DataTemplate x:Key="ItemTemplate"&gt;
      &lt;Border&gt;
        &lt;Grid&gt;
          &lt;!-- lots of stuff go here --&gt;
          &lt;Button Click="MyButton_Click"&gt;Clickme&lt;/Button&gt;
        &lt;/Grid&gt;
      &lt;/Border&gt;
    &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;ListView x:Name="_listView"
    ItemTemplate="{StaticResource ItemTemplate}"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p><strong>C# Code behind</strong></p>
  
  <pre><code>void MyButton_Click(object sender, RoutedEventArgs e)
  {
    MessageBox.Show( string.Format( "clicked on {0}",  
      this._listView.SelectedItem.ToString() ) ) ;
  }
  </code></pre>