title: wpf TreeViewItem bound IsSelected property not working programmatically
body: <p>I'm using MVVM to bind to a tree. I'm doing this in what appears to be a fairly standard way, that is each node object in the tree supports an ITreeNode interface:</p>
  
  <pre><code>public interface ITreeNode
  {
      bool IsExpanded { get; set; }
      bool IsSelected { get; set; }
  
      ObservableCollection&lt;ITreeNode&gt; Children { get; }
  
      ITreeNode Parent { get; set; }
  
      string Name {get;}
  }
  </code></pre>
  
  <p>The xaml defines a simple HierachicalDataTemplate:</p>
  
  <pre><code>&lt;TreeView.ItemTemplate&gt;
      &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
          &lt;TextBlock Text="{Binding Name}" /&gt;
      &lt;/HierarchicalDataTemplate&gt;
  &lt;/TreeView.ItemTemplate&gt;
  </code></pre>
  
  <p>And then I define an ItemContainerStyle to bind the IsExpanded and IsSelected properties</p>
  
  <pre><code>&lt;TreeView.ItemContainerStyle&gt;
      &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
          &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
          &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
          &lt;Setter Property="FontWeight" Value="Normal" /&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsSelected" Value="True"&gt;
                  &lt;Setter Property="FontWeight" Value="Bold" /&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/TreeView.ItemContainerStyle&gt;
  </code></pre>
  
  <p>My issue is, if I programatically change the IsSelected property, the tree does not reflect the new selection.  If I make the selection using the mouse then the IsSelected Property is updated just fine.  I added an event into the setter for the IsSelected property and I can see that when I make a selection with the mouse, I correctly get 2 calls to the IsSelected Setter, the first false to deselect the existing item and the second True for the newly selected node.  When I do it programatically, I only get the single call, which sets the value to true.  I've tried manually setting the old node to false, but it makes not he slightest difference.</p>
  
  <p>The IsExpanded property is working just fine. In fact, if I try to select a node several layers down, the calls to expand all the parent nodes so that the node can be seen, all works just fine, however, the actual node is not selected.</p>
  
  <p>I've been through the code a dozen times and it looks fine, what could be going on here?</p>
  
  <p>Thanks</p>