title: WPF Listview: Detect when listviewitem is selected and then check it
body: <p>I have below listview:</p>
  
  <pre><code>&lt;ListView Margin="10" Name="lvUsers" AlternationCount="2" SelectionMode="Extended"&gt;
  
  &lt;ListView.View&gt;
      &lt;GridView&gt;
          &lt;!-- Checkbox header --&gt;
              &lt;GridViewColumn&gt;
  
                  &lt;GridViewColumn.Header&gt;
                      &lt;CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/&gt;
                  &lt;/GridViewColumn.Header&gt;
  
                  &lt;GridViewColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;CheckBox IsChecked="{Binding IsChecked}" /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/GridViewColumn.CellTemplate&gt;
              &lt;/GridViewColumn&gt;
  
              &lt;GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" /&gt;
          &lt;GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" /&gt;
          &lt;GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" /&gt;
      &lt;/GridView&gt;
  &lt;/ListView.View&gt;
  
      &lt;!-- SELECTED ITEM EVENT --&gt;
      &lt;ListView.ItemContainerStyle&gt;
          &lt;Style TargetType="ListViewItem"&gt;
              &lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_MouseLeftButtonDown" /&gt;
          &lt;/Style&gt;
      &lt;/ListView.ItemContainerStyle&gt;
  
  &lt;/ListView&gt;
  </code></pre>
  
  <p>and code-behind for the event:</p>
  
  <pre><code>    private void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          var item = sender as ListViewItem;
          if (item != null &amp;&amp; item.IsSelected)
          {
              //Do your stuff
          }
  
      }
  </code></pre>
  
  <p>and this the data model:</p>
  
  <pre><code>public class User : INotifyPropertyChanged
  {
      private bool isChecked = false;
      private string name = string.Empty;
      private int age = 0;
      private string mail = string.Empty;
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void NotifyPropertyChanged(String propertyName)
      {
          var handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      public bool IsChecked {
          get
          {
              return this.isChecked;
          }
  
          set
          {
              if (value != this.isChecked)
              {
                  this.isChecked = value;
                  NotifyPropertyChanged("IsSelected");
              }
          }
      }
  
      public string Name
      {
          get
          {
              return this.name;
          }
  
          set
          {
              if (value != this.name)
              {
                  this.name = value;
                  NotifyPropertyChanged("Name");
              }
          }
      }
  
      public int Age {
          get
          {
              return this.age;
          }
  
          set
          {
              if (value != this.age)
              {
                  this.age = value;
                  NotifyPropertyChanged("Age");
              }
          }
      }
  
      public string Mail {
          get
          {
              return this.mail;
          }
  
          set
          {
              if (value != this.mail)
              {
                  this.mail = value;
                  NotifyPropertyChanged("Mail");
              }
          }
      }
  }
  </code></pre>
  
  <p>I have a checkbox at listview header and a checbox for each listview item.</p>
  
  <p>I am trying to detect when a listviewitem is selected and then once selected I want to mark it as checked. Listviewitem event PreviewMouseLeftButtonDown does not work, when fired the item.IsSelected is false, because it is a preview before left mouse button down occurred. There is no MouseClick event, only MouseDoubleClick.</p>
  
  <p>Also, once listviewitem clicked I want to mark as checked (checkbox checked) the item being selected. </p>
  
  <p>How can I do this?</p>