TreeView does not support this kind of tracking the selected item (how should it be done whatsoever...you would have to deal with many instances of ICollectionView, as one DataItem has a collection of children, which themeself could have a collection of children, etc.).
Instead you could bind the TreeView's
SelectedItem [
^]or
SelectedValue [
^]property to a property of your ViewModel.
This way you would track the selected item regardless of the containing collection (that is: the depth in the tree). This solution is ok, if your DataItems are all of the same type and gets uglier, when you have to deal with various Types. Also, multi selection can not be accomplished using this approach.
Alternatively, introduce an IsSelected-property to your DataItem and bind the IsSelected-property of the TreeView's ItemContainer to it:
<TreeView>
<TreeView.ItemContainerStyle>
<Style
TargetType="TreeViewItem">
<Setter
Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}"></Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
with this apporach, you would have support for multiselect and changes to your data items would reflect in the UI (if the IsSelected-property makes use of
INotifyPropertyChanged or is implemented as a
DependencyProperty)
only getting a list of selected items is ugly, as you would have to travers the complete tree of data items and gather all that have IsSelected == true.
hope it helped