tags:

views:

476

answers:

2

I am using a presentation model to implement navigation in my app with the treeview control. I have the treeviewitem's IsSelected property bound to my view model via two-way binding. When a node that has children is selected, I want the first child of that node to be selected instead of the one that is clicked. It seems like the treeviewitem doesn't listen to the property changed event when it is setting the IsSelected property on my presentation model. The first child node is selected, but the parent node doesn't unselect. Here is the code from my presentation model.

public bool IsSelected {
    get {
        return this._isSelected;
    }
    set {
        if(this._isSelected != value) {
            this._isSelected = value;
            if(this.Nodes.Count > 0) {
                this._isSelected = false;
                this.Nodes[0].IsSelected = true;
            }
            this.NotifyPropertyChanged("IsSelected");
        }
    }
}

And here is the style fro my treeviewitem.

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
A: 

I had a very similar problem and i found out that when i select a child by code i need also to give focus to the TreeViewItem wrapper so the parent node will what you called "UnSelect". so in the xaml i added an event handler :

<EventSetter Event="TreeViewItem.Selected" 
                    Handler="TreeViewItem_Selected" 
                        />

And in the partial class :

Private Sub TreeViewItem_Selected(ByVal sender As System.Object, ByVal e As RoutedEventArgs)
            If CType(sender, TreeViewItem) IsNot Nothing Then
                CType(sender, TreeViewItem).Focus()
                e.Handled = True
            End If
End Sub
Amir Inbar
A: 

Another possibility to consider:

TreeView attempts to make sure it is impossible for two TreeViewItems in the tree to be selected at once, so it should prevent this from happening. Perhaps it is a bug in TreeView, but it could also be a problem with the way you are using TreeView.

For efficiency's sake, TreeView is very picky about how it finds the TreeViewItems under it. The algorithm is effectively this (done in an incremental fashion):

  1. Look among my item containers for TreeView objects
  2. Look among those TreeView objects' item containers for more TreeView objects
  3. Repeat step 3 until no more TreeView objects found

Because of this, TreeView can only find its descendants if each level of TreeViewItems is directly under the previous one.

For example, this would work:

<TreeView>
  <TreeViewItem>
    <TreeViewItem />
  </TreeViewItem>
  ...
</TreeView>

and so would this:

<HierarchicalDataTemplate TargetType="{x:Type MyItemType"} ItemsSource="{Binding subItems}">
  ...
</HierarchicalDataTemplate>

<TreeView ItemsSource="{Binding items}" />

But it won't work if non-TreeViewItems are interposed, like this:

<TreeView>
  <TreeViewItem>
    <Border>
      <TreeViewItem/>
    </Border>
  </TreeViewItem>
</TreeView>

or this:

<TreeView>
  <DockPanel>
    <TreeViewItem>
      <TreeViewItem/>
    </TreeViewItem>
  </DockPanel>
</TreeView>

These last two cases will display just fine, but the TreeView will not see the TreeViewItems so its selection code will be disabled. This would cause the symptoms you describe.

I don't know whether this is the situation in your case or not, but I thought I should mention it just in case.

Ray Burns