title: How to deselect ListViewItem programmatically?
body: <p>I'm stumped with this little problem for a while. The <em>ListView</em> under W10 has some strange behavior I cannot understand. Consider a simple list:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ListView Name="myList" ItemClick="myList_ItemClick" SelectionChanged="myList_SelectionChanged" IsItemClickEnabled="True"&gt;
      &lt;ListView.Items&gt;
          &lt;TextBlock Text="First item"/&gt;
          &lt;TextBlock Text="Second item"/&gt;
          &lt;TextBlock Text="Third item"/&gt;
          &lt;TextBlock Text="Fifth item"/&gt;
      &lt;/ListView.Items&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>and the code behind:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void myList_ItemClick(object sender, ItemClickEventArgs e)
  {
      Debug.WriteLine("Clicked item");
      ListView list = sender as ListView;
      ListViewItem listItem = list.ContainerFromItem(e.ClickedItem) as ListViewItem;
      listItem.IsSelected = !listItem.IsSelected;
  }
  
  private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
      Debug.WriteLine($"Selection changed -&gt; added = {e.AddedItems.Count}, removed = {e.RemovedItems.Count}, currently selected = {myList.SelectedItems.Count}");
  }
  </code></pre>
  
  <p>The code above runs very well on W8.1, but on W10 I encounter a problem. Selecting a first item works ok, changing it also, but I cannot deselect an item - listview seems (from debug output) to deselect item and then select it again right away - you can see <em>SelectionChanged</em> event fired twice. Here is sample debug output:</p>
  
  <p><a href="https://i.stack.imgur.com/Fr4P4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fr4P4.jpg" alt="Debug Output"></a></p>
  
  <p>Does somebody know what is going on? How to deselect the item in this case?</p>